Complete course and modules using Rails 5 assign to user - ruby-on-rails

Edit #2
Here is the courses controller
class CoursesController < ApplicationController
layout proc { user_signed_in? ? "dashboard" : "application" }
before_action :set_course, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
before_action :authorize_admin, except: [:index, :show, :complete]
def index
#courses = Course.all.order(created_at: :asc)
end
def show
course = Course.friendly.find(params[:id])
#course_modules = course.course_modules.order(created_at: :asc)
end
def new
#course = Course.new
end
def edit
end
def create
#course = Course.new(course_params)
respond_to do |format|
if #course.save
format.html { redirect_to courses_path, notice: 'Course was successfully created.' }
format.json { render :show, status: :created, location: courses_path }
else
format.html { render :new }
format.json { render json: #course.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #course.update(course_params)
format.html { redirect_to #course, notice: 'Course was successfully updated.' }
format.json { render :show, status: :ok, location: #course }
else
format.html { render :edit }
format.json { render json: #course.errors, status: :unprocessable_entity }
end
end
end
def destroy
#course.destroy
respond_to do |format|
format.html { redirect_to courses_url, notice: 'Course was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_course
#course = Course.friendly.find(params[:id])
end
def course_params
params.require(:course).permit(:title, :summary, :description, :trailer, :price)
end
end
Edit #1
So going off Jagdeep's answer below I have now done the following:
course.rb
class Course < ApplicationRecord
extend FriendlyId
friendly_id :title, use: :slugged
has_many :course_modules
validates :title, :summary, :description, :trailer, :price, presence: true
def complete?
self.update_attribute(:complete, true)
end
end
course_modules_user.rb
class CourseModulesUser < ApplicationRecord
belongs_to :course_module
belongs_to :user
def complete!
self.update_attribute(:complete, true)
end
end
courses_user.rb
class CoursesUser < ApplicationRecord
belongs_to :course
belongs_to :user
end
user.rb
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable
has_one_attached :avatar
has_many :courses_users
has_many :courses, through: :courses_users
has_many :course_modules_users
has_many :course_modules, through: :course_modules_users
def mark_course_module_complete!(course_module)
self.course_modules_users
.where(course_module_id: course_module.id)
.first
.complete!
end
def after_confirmation
welcome_email
super
end
protected
def welcome_email
UserMailer.welcome_email(self).deliver
end
end
Migrations
class CreateCoursesUsers < ActiveRecord::Migration[5.2]
def change
create_table :courses_users do |t|
t.integer :course_id
t.integer :user_id
t.boolean :complete
t.timestamps
end
end
end
class CreateCourseModulesUsers < ActiveRecord::Migration[5.2]
def change
create_table :course_modules_users do |t|
t.integer :course_module_id
t.integer :user_id
t.boolean :complete
t.timestamps
end
end
end
However, I'm getting errors like this
Original Question
So this is a continuation of a previous question, however, this will stray off from the topic of that so here is a new one.
After this, I got roughly what I wanted to get working which is allowing people to mark off modules and make the course complete if all modules are complete. However, upon testing a new user the modules and courses are being marked as complete (obviously a new user isn't going to complete the course on sign-in, nor are any modules going to be complete) so I need for all users to be separate in terms of what is marked as complete and what isn't.
Previously a user by the name of #engineersmnky mentioned HABTM relationship, however, I've not dealt with this previously.
Here is how I have things setup thus far:
user.rb
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable
has_one_attached :avatar
has_many :courses
def after_confirmation
welcome_email
super
end
protected
def welcome_email
UserMailer.welcome_email(self).deliver
end
end
course.rb
class Course < ApplicationRecord
extend FriendlyId
friendly_id :title, use: :slugged
has_many :users
has_many :course_modules
validates :title, :summary, :description, :trailer, :price, presence: true
def complete!
update_attribute(:complete, true)
end
end
course_module.rb
class CourseModule < ApplicationRecord
extend FriendlyId
friendly_id :title, use: :slugged
belongs_to :course
has_many :course_exercises
validates :title, :course_id, presence: true
scope :completed, -> { where(complete: true) }
after_save :update_course, if: :complete?
private
def update_course
course.complete! if course.course_modules.all?(&:complete?)
end
end
if the course is complete conditional courses/index.html.erb
<% if course.complete? %>
<%= link_to "Completed", course, class: "block text-lg w-full text-center text-white px-4 py-2 bg-green hover:bg-green-dark border-2 border-green-dark leading-none no-underline" %>
<% else %>
<%= link_to "View Modules", course, class: "block text-lg w-full text-center text-grey-dark hover:text-darker px-4 py-2 border-2 border-grey leading-none no-underline hover:border-2 hover:border-grey-dark" %>
<% end %>
if course module is complete conditional courses/show.html.erb
<% if course_module.complete? %>
<i class="fas fa-check text-green float-left mr-1"></i>
<span class="text-xs mr-2">Completed</span>
<% else %>
<%= link_to complete_course_module_path(course_module), method: :put do %>
<i class="fas fa-check text-grey-darkest float-left mr-2"></i>
<% end %>
Databases
Course Modules
Courses

You will need to create new tables courses_users and course_modules_users to distinguish between courses/course_modules of different users.
Remove field complete from tables courses and course_modules. We don't want to mark a course/course_module as completed globally. See this for how to use migrations to do that.
Further, define has_many :through associations between users and course/course_modules as below:
class User < ApplicationRecord
has_many :courses_users
has_many :courses, through: :courses_users
has_many :course_modules_users
has_many :course_modules, through: :course_modules_users
end
class Course < ApplicationRecord
has_many :course_modules
end
class CoursesUser < ApplicationRecord
# Fields:
# :course_id
# :user_id
# :complete
belongs_to :course
belongs_to :user
end
class CourseModule < ApplicationRecord
belongs_to :course
end
class CourseModulesUser < ApplicationRecord
# Fields:
# :course_module_id
# :user_id
# :complete
belongs_to :course_module
belongs_to :user
end
Now, the queries can be made this way:
Course.all
=> All courses
Course.find(1).course_modules
=> All course modules of a course
user = User.find(1)
course = Course.find(1)
# Assign `course` to `user`
user.courses_users.create(course_id: course.id)
user.courses
=> [course]
course_module = CourseModule.find(1)
# Assign `course_module` to `user`
user.course_modules_users.create(course_module_id: course_module.id)
user.course_modules
=> [course_module]
Now, to mark a course module complete for a user, do this:
class User < ApplicationRecord
def mark_course_module_complete!(course_module)
self.course_modules_users
.where(course_module_id: course_module.id)
.first
.complete!
end
end
class CourseModulesUser < ApplicationRecord
def complete!
self.update_attribute(:complete, true)
end
end
course_module = CourseModule.find(1)
user.mark_course_module_complete!(course_module)
Similarly for courses:
class User < ApplicationRecord
def mark_course_complete!(course)
self.courses_users
.where(course_id: course.id)
.first
.complete!
end
end
class CoursesUser < ApplicationRecord
def complete!
self.update_attribute(:complete, true)
end
end
This should solve your issue of marking courses and course modules as completed on user basis.
There are other things to consider to make it completely functional which i will leave for you to implement e.g. marking a user's course complete automatically when all course modules of a user are complete (Yes, you need to fix that again), marking a user's course incomplete if at least one of its course modules get incompleted, etc.
SO is always open, if you get stuck again.

Undefined method complete? for CourseModule:0x000..
I will focus on the error and explain the reason for it. You are calling complete? on a course_module here <% if course_module.complete? %>. But you don't have a method called complete? in the CourseModule model. That explains why the error has triggered.
You should define it in the CourseModule to avoid the error
class CourseModule < ApplicationRecord
def complete?
#your logic here
end
end
Note:
If you are willing to try a different approach, I will recommend you to have a go with enums. enums are very powerful and serves with in-built methods which comes very handy.
For example, you can change the CourseModel to below with enums
class CourseModule < ApplicationRecord
extend FriendlyId
friendly_id :title, use: :slugged
enum status: [ :completed, :not_completed ]
belongs_to :course
has_many :course_exercises
.......
end
By this you can simply call course_module.completed? which returns true or false based on the status of the course_module. And to update a course_module status as completed, just call course_module.completed!

Related

Why does the JSON patch data from relationships not get saved via Ruby on Rails?

I have an Ruby on Rails api where the data is handled in JSON. When I want to update an entity all the attributes are getting updated persistently but changed relationships arent' getting handled correctly, the entity stays the same as before.
JSON data before and after the PATCH:
{"data":{"id":"26","type":"candidate","attributes":
{"place":"Ort","zip_code":"PLZ","address":"Adresse",
"date_of_birth":"2019-01-01T00:00:00.000Z","title":"Frau",
"first_name":"Vorname","last_name":"Nachname",
"email_address":"email#example.ch",
"confirm_terms_and_conditions":true},"relationships":
{"occupational_fields":{"data":[]}}}}
PATCH input:
Started PATCH "/candidates/26" for 127.0.0.1 at 2019-01-22
19:40:53 +0100
Processing by CandidatesController#update as JSON
Parameters: {"data"=>{"id"=>"26", "attributes"=>{"place"=>"Ort",
"zip_code"=>"PLZ", "address"=>"Adresse", "title"=>"Frau",
"first_name"=>"Vorname", "last_name"=>"Nachname",
"email_address"=>"email#example.ch",
"confirm_terms_and_conditions"=>true, "date_of_birth"=>"2019-01-
01T00:00:00.000Z"}, "relationships"=>{"occupational_fields"=>
{"data"=>[{"type"=>"occupational-fields", "id"=>“4“}]}},
"type"=>"candidates"}, "id"=>"26", "candidate"=>{}}
This are my models, Candidates and OccupationalFields are related via a has_many belongs_to_many relationship through one CandidatesOccupationalField:
class Candidate < ApplicationRecord
has_many :candidates_occupational_fields, dependent: :destroy
has_many :occupational_fields, through:
:candidates_occupational_fields, dependent: :nullify
end
class CandidatesOccupationalField < ApplicationRecord
belongs_to :candidate
belongs_to :occupational_field
end
class OccupationalField < ApplicationRecord
has_many :candidates_occupational_fields, dependent: :destroy
has_many :candidates, through: :candidates_occupational_fields,
dependent: :nullify
end
This is the used controller:
class CandidatesController < ApplicationController
before_action :set_candidate, only: %i[show update destroy]
# GET /candidates
def index
#candidates = Candidate.all
render json: CandidateSerializer.new(#candidates).serialized_json
end
# GET /candidates/1
def show
#candidate = Candidate.find(params[:id])
render json: CandidateSerializer.new(#candidate).serialized_json
end
# POST /candidates
def create
#candidate = Candidate.new(candidate_params)
if #candidate.save
render json: CandidateSerializer.new(#candidate), status: :created
else
render json: #candidate.errors, status: :unprocessable_entity
end
end
# PATCH/PUT /candidates/1
def update
#candidate = Candidate.find(params[:id])
if #candidate.update(candidate_params)
render json: CandidateSerializer.new(#candidate)
else
render status: :unprocessable_entity
end
end
# DELETE /candidates/1
def destroy
#candidate.destroy
end
private
# Use callbacks to share common setup or constraints between actions.
def set_candidate
#candidate = Candidate.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def candidate_params
params.require(:data)[:attributes]
.permit(:place, :zip_code, :address,
:date_of_birth, :title, :first_name,
:last_name, :email_address,
:confirm_terms_and_conditions,
occupational_field_ids: [])
end
end
The JSON formatting is handled by fastjsonapi, this are the used serializers:
class CandidateSerializer
include FastJsonapi::ObjectSerializer
attributes :place, :zip_code, :address, :date_of_birth,
:title, :first_name, :last_name, :email_address,
:confirm_terms_and_conditions
has_many :occupational_fields
end
class OccupationalFieldSerializer
include FastJsonapi::ObjectSerializer
attributes :field
has_many :candidates
end
Thank you for your help.
The problem was, that the used serializer fast_jsonapi can't be used as deserializer and the Rail's strong parameters can't handle the json input. It works with the gem restful-jsonapi and modified params as shown in the example of the readme of restful-jsonapi.

Rails Rollback Transaction

I have problems with my Rails Block. After I implemented a comment-section I am not able to create posts anymore. The console gives me a rollback transaction. So I did
p = Post.new
p.valid? # false
p.errors.messages
It seems I have some validation problems with user :user=>["must exist"]. But before I implemented comments it did work. Can someone help me out?
User.rb
class User < ApplicationRecord
has_many :posts
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
Post.rb
class Post < ApplicationRecord
belongs_to :user
has_many :comments, dependent: :destroy
validates :title, presence: true, length: {minimum: 5}
validates :body, presence: true
has_attached_file :image #, :styles => { :medium => "300x300>", :thumb => "100x100>" }
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end
Post-migrate
class CreatePosts < ActiveRecord::Migration[5.1]
def change
create_table :posts do |t|
t.string :title
t.text :body
t.timestamps
end
end
end
Post_controller
class PostsController < ApplicationController
def index
#posts = Post.all.order("created_at DESC")
end
def new
#post = Post.new
end
def create
#post = Post.new(post_params)
if #post.save
redirect_to #post
else
render 'new'
end
end
def show
#post = Post.find(params[:id])
end
def edit
#post = Post.find(params[:id])
end
def update
#post = Post.find(params[:id])
if #post.update(post_params)
redirect_to #post
else
render 'edit'
end
end
def destroy
#post = Post.find(params[:id])
#post.destroy
redirect_to posts_path
end
private
def post_params
params.require(:post).permit(:title, :body, :theme)
end
end
When you are creating a post you need to assign a user to that post in the create method under your posts controller. You could try something like this.
def create
if current_user
#post.user_id = current_user.id
end
## More create method stuff
end
By default, in a belongs_to association a user is required to create the post otherwise you will not be able to create the post. Since, from the looks of it, you do not have anything that assigns the user to that post in the create method.

Why am i getting two different urls for the same "story" in my rails app?

I recently created a profile page for the users of the app am developing but along the way i noticed that the URL for the story originally is a little different from the same story on the users page. e.g normal url is "http://localhost:3000/genres/action/stories/37" while it is "http://localhost:3000/genres/absurdist/stories/37", meanwhile the story originally belongs to "action genre" and not "absurdist". Meanwhile, the two urls directs to the normal story page.
genre.rb
class Genre < ApplicationRecord
belongs_to :user
has_many :stories, dependent: :destroy
is_impressionable
extend FriendlyId
friendly_id :name, use: :slugged
def should_generate_new_friendly_id?
name_changed?
end
end
story.rb
class Story < ApplicationRecord
belongs_to :genre
belongs_to :user
has_many :episodes, dependent: :destroy
is_impressionable
extend FriendlyId
friendly_id :title, use: :slugged
def should_generate_new_friendly_id?
title_changed?
end
has_attached_file :image, size: { less_than: 1.megabyte }, styles: { medium: "300x300#", wide: "200x400#" }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
scope :of_followed_users, -> (following_users) { where user_id: following_users }
end
profiles_controller
class ProfilesController < ApplicationController
before_action :find_user
before_action :find_genre
before_action :owned_profile, only: [:edit, :update]
before_action :authenticate_user!
def show
#stories = User.find_by(user_name: params[:user_name]).stories.order('created_at DESC')
impressionist(#user)
end
def edit
end
def update
if #user.update(profile_params)
flash[:success] = 'Your profile has been updated.'
redirect_to profile_path(#user.user_name)
else
#user.errors.full_messages
flash[:error] = #user.errors.full_messages
render :edit
end
end
private
def profile_params
params.require(:user).permit(:avatar, :bio)
end
def find_user
#user = User.find_by(user_name: params[:user_name])
end
def find_genre
#genre = Genre.friendly.find_by(params[:slug])
end
def owned_profile
#user = User.find_by(user_name: params[:user_name])
unless current_user == #user
flash[:alert] = "That profile does not belong to you"
redirect_to root_path
end
end
end
profiles show
<div class="container my-5">
<div class="card-columns clearfix">
<%= render #user.stories %>
</div>
</div>

Rails Client side Collection Validation fails - Simple Form

I have to build a simple app that allows users to loan and borrow books. Simply put a User can create books, and they can pick another user to loan the book to.
I have three models User, Book and Loan:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :books
has_many :loans, through: :books
has_many :borrowings, class_name: "Loan"
validates :username, uniqueness: true
validates :username, presence: true
end
class Book < ActiveRecord::Base
belongs_to :user
has_many :loans
validates :title, :author, presence: true
end
class Loan < ActiveRecord::Base
belongs_to :user
belongs_to :book
validates :user, :book, :status, presence: true
end
The LoansController looks like this:
class LoansController < ApplicationController
before_action :find_book, only: [:new, :create]
def new
#users = User.all
#loan = Loan.new
authorize #loan
end
def create
#loan = Loan.new
#loan.book = #book
#loan.user = User.find(loan_params[:user_id])
#loan.status = "loaned"
authorize #loan
if #loan.save
redirect_to :root
else
render :new
end
end
private
def loan_params
params.require(:loan).permit(:user_id)
end
def find_book
#book = Book.find(params[:book_id])
end
end
My form looks like:
<%= simple_form_for([#book, #loan]) do |f| %>
<%= f.input :user_id, collection: #users.map { |user| [user.username, user.id] }, prompt: "Select a User" %>
<%= f.submit %>
<% end %>
If I submit the form without selecting a user, and keep the "Select a User" prompt option, the form is submitted and the app crash because it can't find a user with id=
I don't know why the user presence validation in the form does not work...
you will change your Create method
def create
#loan = Loan.new
#loan.book = #book
#loan.user = User.find_by_id(loan_params[:user_id])
#loan.status = "loaned"
authorize #loan
if #loan.save
redirect_to :root
else
render :new
end
end

After_create following relationship after Project Creation not working

I'm trying to allow users to create projects...and as soon as a user creates a project...they will automatically be following that project. (I have my app setup to allow a user to follow a project from a 'follow' button on the project profile). I would like the project creator to automatically be following the new project without having to click the 'follow' button. I rearranged my code as per Bilal's answer...but now clicking 'create project' simply refreshes the 'new' view (no project gets posted). I assumed this has to do with the Pundit authorizations but perhaps someone can clarify why the 'create' action is no longer working...
My Projects Model:
class Project < ActiveRecord::Base
belongs_to :owner, :foreign_key=>'user_id', :class_name=>'User'
has_many :reverse_relationships, foreign_key: "followed_id",
class_name: "Relationship",
dependent: :destroy
has_many :followers, through: :reverse_relationships, source: :follower
validates :title, presence: true
validates :background, presence: true
validates :projectimage, presence: true
mount_uploader :projectimage, ProjectimageUploader
attr_accessor :crop_x, :crop_y, :crop_w, :crop_h
after_update :crop_projectimage
def crop_projectimage
projectimage.recreate_versions! if crop_x.present?
end
def private?
self.is_private == true
end
def public?
self.is_private == false
end
end
Relationships Model:
class Relationship < ActiveRecord::Base
belongs_to :follower, class_name: "User"
belongs_to :followed, class_name: "Project"
validates :follower_id, presence: true
validates :followed_id, presence: true
enum role: [:admin, :collaborator, :visitor]
after_initialize :set_default_role, :if => :new_record?
def set_default_role
self.role ||= :visitor
end
end
My Projects Controller:
class ProjectsController < ApplicationController
before_filter :authenticate_user!, only: [:create, :new, :edit, :update, :delete, :followers]
# CREATES REDIRECT & ALERT MESSAGE WHEN PUNDIT SEES SOMEONE IS NOT AUTHORIZED (via :not_authorized_in_project below)
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
def new
#project = Project.new
end
def show
#project = Project.find(params[:id])
authorize #project, :visit?
# #user = User.where(:id => #project.user_id).first
rescue Pundit::NotAuthorizedError
flash[:warning] = "You are not authorized to access this page."
redirect_to project_path || root_path
end
def index
#projects = policy_scope(Project).all
end
def create
#project = current_user.own_projects.build(project_params)
#project.followers << current_user
if #project.save
if params[:project][:projectimage].present?
render :crop
else
flash[:success] = "You've successfully created a Project..."
redirect_to #project
end
else
render 'new'
end
end
def update
#project = Project.find(params[:id])
if #project.update_attributes(project_params)
if params[:project][:projectimage].present?
render :crop
else
flash[:success] = "Project Created"
redirect_to #project
end
else
render 'edit'
end
end
def destroy
User.find(params[:id]).destroy
flash[:success] = "Project destroyed"
redirect_to users_path
end
def followers
#title = "Following this Project"
#project = Project.find(params[:id])
#project = #project.followers.paginate(page: params[:page])
render 'show_follow_project'
end
private
def project_params
params.require(:project).permit(:title, :background, :is_private, :projectimage, :user_id, :crop_x, :crop_y, :crop_w, :crop_h)
end
def user_not_authorized
flash[:warning] = "You are not authorized to access this page."
redirect_to project_path(#project) || root_path
end
end
My User Model:
class User < ActiveRecord::Base
has_many :own_projects, :class_name=>'Project'
has_many :projects
has_many :relationships, foreign_key: "follower_id", dependent: :destroy
has_many :followed_projects, through: :relationships, source: :followed
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
def following?(some_project)
relationships.find_by_followed_id(some_project.id)
end
def follow!(some_project)
self.relationships.create!(followed_id: some_project.id)
end
def unfollow!(some_project)
relationships.find_by_followed_id(some_project.id).destroy
end
Pundit Project Policy:
class ProjectPolicy < Struct.new(:user, :project)
class Scope < Struct.new(:user, :scope)
# SCOPE & RESOLVE METHOD USED TO RESTRICT PROJECTS INDEX TO PUBLIC & THOSE YOU'RE AN ADMIN/COLLAB ON
def resolve
followed_project_ids = user.followed_projects.map(&:id)
public_project_ids = Project.where(:is_private=>false).map(&:id)
Project.where(:id=>followed_project_ids + public_project_ids)
end
end
def update?
user.project_admin? || user.project_collaborator?
end
# METHOD USED IN PROJECTS_CONTROLLER (SHOW) TO RESTRICT VISITING PRIVATE PROJECT PROFILES TO ADMINS & COLLABS
def visit?
user.project_admin?(project) || user.project_collaborator?(project)
end
end
It's never a good idea to use current_user in a model, see this for reference.
Any easy and efficient place to set this thing would be the controller itself. So, you can write the following code:
def create
#project = current_user.own_projects.build(project_params)
#project.followers << current_user
if #project.save
if params[:project][:projectimage].present?
render :crop
else
flash[:success] = "You've successfully created a Project..."
redirect_to #project
end
else
render 'new'
end
end

Resources