Having trouble with create method in CommentsController - ruby-on-rails

This is my current Create method in CommentsController
def create
#place = Place.find(params[:place_id])
#comment = #place.comments.create(comment_params)
#comment.user = current_user
if #comment.save
redirect_to place_path(#place)
else
render "comments/_form"
end
end
I was told by someone that this hits the database twice. Upon checking the logs, this is the result:
Started GET "/places/9" for 127.0.0.1 at 2015-01-22 15:01:47 -0800
Processing by PlacesController#show as HTML
Parameters: {"id"=>"9"}
Place Load (0.3ms) SELECT "places".* FROM "places" WHERE "places"."id" = ? LIMIT 1 [["id", 9]]
Comment Load (0.2ms) SELECT "comments".* FROM "comments" WHERE "comments"."place_id" = ? [["place_id", 9]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Rendered comments/_comment.html.erb (8.0ms)
Rendered comments/_form.html.erb (2.7ms)
Rendered places/show.html.erb within layouts/application (73.3ms)
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Completed 200 OK in 532ms (Views: 529.4ms | ActiveRecord: 1.0ms)
So obviously, I don't want to be inefficient in my code.
This is what the person suggested instead:
comment_params[:user_id] = current_user.id
if #places.comments.create(comment_params)
.....
else
....
end
So ... I rewrote the Create method to this:
def create
#place = Place.find(params[:place_id])
#comment = #place.comments.create(comment_params)
comment_params[:user_id] = current_user.id
if #places.comments.create(comment_params)
redirect_to place_path(#place)
else
render "comments/_form"
end
end
Upon the rewritten Create method, I keep getting this error when I try to leave a comment: undefined method comments for nil:NilClass
Help me to understand how to rewrite this Create method correctly, please?
SideNote - Not sure if it's relevant if yes, please address it, if no, please ignore
Upon checking the last comment in the rails console, I was startled to see that the user_id is nil, whereas for place, it's not.
Place belongs to User
Comment belongs to User
User has many of both Place and Comment

You're calling create twice, which would result in the comment being posted twice, which is probably not what you have intended.
The first time you call create, it has no user_id assigned, that's the reason the user_id is set to null in the database (hint: use a presence validation on the user_id in your comment model).
The second time you call create you call it on #places instead of #place (typo). This causes your nil error.
Here is another way to solve your problem:
def create
#place = Place.find(params[:place_id])
#comment = #place.comments.build(comment_params)
#comment.user_id = current_user.id
if #comment.save(comment_params)
redirect_to place_path(#place)
else
render "comments/_form"
end
end
The difference is, that we first "build" a comment without creating it, but it will be filled with the ID of the place etc.
In a next step we add the current user's id and then we call save on the object, which hits the database.

Related

Confusion in object ID when editing and deleting

Specify the ID of the desired object, while the first object of the model is always deleted/edited. For the first time I met such a problem, checked everything. What's wrong?
Started DELETE "/questions/6" for 127.0.0.1 at 2018-07-05 10:48:13 +0300
Processing by QuestionsController#destroy as HTML
Parameters: {"authenticity_token"=>"luh7ShhQ9pWka7wmWMnG4WMQVAnKRjtAJwn0s5at8/GBDOtFjUwZEF70o8hOFOaAN+pVB592V1+egH/PDJVUVA==", "id"=>"6"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
↳ /Users/gorbunov/.rvm/gems/ruby-2.4.1/gems/activerecord-5.2.0/lib/active_record/log_subscriber.rb:98
Question Load (0.1ms) SELECT "questions".* FROM "questions" WHERE (6) LIMIT ? [["LIMIT", 1]]
↳ app/controllers/questions_controller.rb:86
(0.0ms) begin transaction
↳ app/controllers/questions_controller.rb:45
Question Destroy (0.4ms) DELETE FROM "questions" WHERE "questions"."id" = ? [["id", 3]]
↳ app/controllers/questions_controller.rb:45
(1.6ms) commit transaction
↳ app/controllers/questions_controller.rb:45
Redirected to http://localhost:3000/questions
Completed 302 Found in 7ms (ActiveRecord: 2.4ms)
I want to delete an object with ID =6, but delete an object with ID = 3
Action Destroy in controller:
def destroy
#question = Question.find_by(params[:id])
#question.destroy
flash[:success] = 'Вопрос успешно удалён!'
redirect_to questions_path
end
Link_to helper for delete object:
<%= link_to qest, class: 'btn btn-outline-danger', method: :delete, data: {confirm: "Хорошо подумал?"} do %>
You are using find_by without mentioning the column. When used without specifying column, find_by uses it as WHERE condition
Change it to Question.find_by(id: params[:id])(returns nil if matching record is not found) or Question.find(params[:id])(raises ActiveRecord::RecordNotFound error if record not found).
This is the clue.
Question Load (0.1ms) SELECT "questions".* FROM "questions" WHERE (6) LIMIT ? [["LIMIT", 1]]
↳ app/controllers/questions_controller.rb:86
This is generated from #question = Question.find_by(params[:id]). Note the WHERE (6). find_by takes either a single argument of raw SQL to use in the where clause, or a column and a value. You've only given it a single argument, so it's interpreted it as SQL and thus WHERE (6). This will be true for every column, so you get some random column, in this case 3, then you destroy it.
Instead you want:
#question = Question.find_by(id: params[:id])
Or better, use find.
#question = Question.find(params[:id])
And if you're just going to destroy it, use destroy on the class.
Question.destroy(params[:id])

Ruby on Rails Update saving to wrong ID

I have this site where a user can have many pets. These pets are listed on there profile, and clicking on a pet takes them to that pets profile, where they have the option to edit the pet. Loading the form for the pets works great, it gets the correct information for each one, but then there's the problem with updating. Clicking submit on the edit form causes all updates to go to the first pet they have. Like, if the have the pets with the IDs 2, 5 and 11, and updates on 5 or 11 go to 2. I'm not quite sure what's going on.
Here are my edit and update actions for the PetsController:
#GET to /users/:user_id/pet/edit
def edit
#user = User.find( params[:user_id] )
#pet = Pet.find_by_id( params[:id] )
end
#PUT to /users/:user_id/pet/
def update
#Retrieve user from database
#user = User.find( params[:user_id] )
#Retrieve user's pet
#pet = #user.pets.find_by( params[:id] )
#Mass assign edited pet attributes and save (update)
if #pet.update_attributes(pet_params)
flash[:success] = "Pet updated!"
#Redirect user to profile page
redirect_to user_path(id: params[:user_id] )
else
render action: :edit
Rails.logger.info(#pet.errors.messages.inspect)
end
end
I've tried different ways of finding the users pet, such as
#user.pet(params[:id])
That resulted in an error of it saying it couldn't find a pet without an ID, so next I tried
#user.pet.find_by_id(params[:id])
But that resulted in update_attribute not being defined, so I assumed I shouldn't be using find_by_id anyways
Is there another way I should be doing this perhaps? I'm thinking the error is that the pet's ID isn't being passed in correctly. If so, what could be a fix for that? I tried passing the ID as a hidden field from the edit form, but that didn't work.
Here's what happens in the terminal when a user tries to update a pet
Processing by PetsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"0tC+qRK01CMCEZWbDtJyZ4QNmXQxnaAPKT+anRKGKh5hTWekTbEHzAd+8 +YOVIWBB0/imVrMVNcpkeBR18SHNw==", "pet"=>{"color"=>"Darigan", "species"=>"Yurble", "gender"=>"Male", "level"=>"25", "hp"=>"50", "strength"=>"50", "defence"=>"50", "movement"=>"50", "uc"=>"1", "rw"=>"0", "rn"=>"0", "uft"=>"1", "ufa"=>"0", "description"=>"uiy7i77i7i", "id"=>"5", "name"=>"Fouany", "hsd"=>"150"}, "user_id"=>"5"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 5], ["LIMIT", 1]]
Pet Load (0.3ms) SELECT "pets".* FROM "pets" WHERE "pets"."user_id" = ? LIMIT ? [["user_id", 5], ["LIMIT", 1]]
(0.1ms) begin transaction
Pet Exists (0.2ms) SELECT 1 AS one FROM "pets" WHERE "pets"."name" = ? AND ("pets"."id" != ?) LIMIT ? [["name", "Fouany"], ["id", 2], ["LIMIT", 1]]
(0.1ms) rollback transaction
Rendering pets/edit.html.erb within layouts/application
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 5], ["LIMIT", 1]]
Rendered pets/edit.html.erb within layouts/application (12.4ms)
Profile Load (0.4ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."user_id" = ? LIMIT ? [["user_id", 5], ["LIMIT", 1]]
{:name=>["has already been taken"], :color=>[], :species=>[], :gender=>[], :level=>[], :hp=>[], :strength=>[], :defence=>[], :movement=>[], :uc=>[], :rw=>[], :rn=>[], :uft=>[], :ufa=>[], :description=>[]}
Completed 200 OK in 202ms (Views: 136.1ms | ActiveRecord: 5.0ms)
As you can see, the pet's ID goes from 5 to 2 back from 5, so maybe that's somewhere to start for fixing the issue?
Any suggestions and advice is greatly appreciated!
#user.pets.find_by(params[:id])
Should be
#user.pets.find_by(id: params[:id])
If your user should have only one pet (the route suggests this), then the pet id is not needed to access it. You can get the pet by
#user = User.find( params[:user_id] )
#pet = #user.pet
Have a look at this comment in your code:
#PUT to /users/:user_id/pet/
It mentions the user_id, but not the id

How is Ajax Destroy method for a nested resource is causing its parent resource to be destroyed?

I have resources set up like this:
resources :scoreboards do
resources :teams
end
On my scoreboard#show view page, I have a collection for the team model which generates a div for each team. Beside each team-div is a Delete button which routes to a method in the teams_controller to delete that team.
Here is a list of all the code associated with it:
The Delete Button next to a team
<div>team example</div> <%= link_to "Del", scoreboard_team_path(#scoreboard, team), remote: true, method: :delete, class: "btn btn-primary" %>
The Teams_controller method for the button
def destroy
#scoreboard = Scoreboard.find(params[:scoreboard_id])
#team = #scoreboard.teams.find(params[:id])
#team.destroy
respond_to do |format|
format.html {redirect_to scoreboard_url(#scoreboard)}
format.js
end
end
The destroy.js.erb file
$( "#team_<%=#team.id%>" ).hide();
Now the issue on hand is that whenever I click on a delete button really fast in quick succession(twice or more on one button), all the Ajax delete buttons stop working. This is most likely because the Scoreboard resource the teams were associated to has been deleted because I get the following error in the Scoreboards_Controller:
NoMethodError in ScoreboardsController#show
undefined method `teams' for nil:NilClass
def show
#scoreboard = Scoreboard.find_by_id(params[:id])
#team = #scoreboard.teams.build # new team form on the page
#comment = #scoreboard.comments.new
#schedule = #scoreboard.schedules.build
end
Then when I check my list of Scoreboards, the #Scoreboard resource which the #team was associated to does not exist anymore. Why is that happening?
Edit: Checking the development logs has clarified what is happening.
So I click delete really fast and it destroys the Team associated with that delete button. Since I manage to click the delete button twice before the destroy.js.erb files processes(hides the deleted div), the Teams_Controller#destroy method is run again on the deleted team, however there is nothing to delete, therefore the Teams_Controller#destroy proceeds to redirect to #scoreboard. Now for some reason, the Scoreboards_Controller#destroy executes and deletes the #scoreboard and after that it attempts to redirect again and encounters a routing error because the scoreboard no longer exists.
Here is some of the log for clarification:
Started DELETE "/scoreboards/45/teams/478" for 99.000.000.000 at 2015-12-12 03:54:09 +0000
Processing by TeamsController#destroy as JS
Parameters: {"scoreboard_id"=>"45", "id"=>"478"}
[1m[36mScoreboard Load (0.3ms)[0m [1mSELECT "scoreboards".* FROM "scoreboards" WHERE "scoreboards"."id" = ? ORDER BY "scoreboards"."created_at" DESC LIMIT 1[0m [["id", 45]]
[1m[35mTeam Load (0.2ms)[0m SELECT "teams".* FROM "teams" WHERE "teams"."scoreboard_id" = ? AND "teams"."id" = ? LIMIT 1 [["scoreboard_id", 45], ["id", 478]]
[1m[36m (0.3ms)[0m [1mbegin transaction[0m
[1m[35mSQL (0.4ms)[0m DELETE FROM "teams" WHERE "teams"."id" = ? [["id", 478]]
[1m[36m (10.6ms)[0m [1mcommit transaction[0m
Rendered teams/destroy.js.erb (0.2ms)
Completed 200 OK in 48ms (Views: 28.1ms | ActiveRecord: 11.8ms)
Started DELETE "/scoreboards/45/teams/478" for 99.000.000.000 at 2015-12-12 03:54:09 +0000
Processing by TeamsController#destroy as JS
Parameters: {"scoreboard_id"=>"45", "id"=>"478"}
[1m[35mScoreboard Load (0.3ms)[0m SELECT "scoreboards".* FROM "scoreboards" WHERE "scoreboards"."id" = ? ORDER BY "scoreboards"."created_at" DESC LIMIT 1 [["id", 45]]
[1m[36mTeam Load (0.2ms)[0m [1mSELECT "teams".* FROM "teams" WHERE "teams"."scoreboard_id" = ? AND "teams"."id" = ? LIMIT 1[0m [["scoreboard_id", 45], ["id", 478]]
Redirected to https://score-app-kpauls.c9.io/scoreboards/45
Completed 302 Found in 19ms (ActiveRecord: 0.5ms)
Started DELETE "/scoreboards/45" for 99.000.000.000 at 2015-12-12 03:54:09 +0000
Processing by ScoreboardsController#destroy as JS
Parameters: {"id"=>"45"}
[1m[35mScoreboard Load (0.2ms)[0m SELECT "scoreboards".* FROM "scoreboards" WHERE "scoreboards"."id" = ? ORDER BY "scoreboards"."created_at" DESC LIMIT 1 [["id", 45]]
[1m[36mUser Load (0.1ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1[0m [["id", 105]]
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 105]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "scoreboards".* FROM "scoreboards" WHERE "scoreboards"."id" = ? ORDER BY "scoreboards"."created_at" DESC LIMIT 1[0m [["id", "45"]]
[1m[35m (0.2ms)[0m begin transaction
[1m[36mTeam Load (0.1ms)[0m [1mSELECT "teams".* FROM "teams" WHERE "teams"."scoreboard_id" = ?[0m [["scoreboard_id", 45]]
[1m[35mSQL (0.3ms)[0m DELETE FROM "teams" WHERE "teams"."id" = ? [["id", 479]]
[1m[36mSQL (0.0ms)[0m [1mDELETE FROM "teams" WHERE "teams"."id" = ?[0m [["id", 480]]
[1m[35mSQL (0.1ms)[0m DELETE FROM "teams" WHERE "teams"."id" = ? [["id", 481]]
[1m[36mSQL (0.1ms)[0m [1mDELETE FROM "teams" WHERE "teams"."id" = ?[0m [["id", 482]]
[1m[35mComment Load (0.1ms)[0m SELECT "comments".* FROM "comments" WHERE "comments"."scoreboard_id" = ? [["scoreboard_id", 45]]
[1m[36mSQL (0.1ms)[0m [1mDELETE FROM "scoreboards" WHERE "scoreboards"."id" = ?[0m [["id", 45]]
[1m[35m (14.1ms)[0m commit transaction
Redirected to https://score-app-kpauls.c9.io/scoreboards
Completed 302 Found in 39ms (ActiveRecord: 15.6ms)
After this the program encounters a routing error. I will continue to look into it but if anyone could help find the reason why scoreboards_controller#destroy is being called, it'd be really appreciated.
Update on the Problem:
So I have figured out the problem. I had these two methods in my application_controller file.
rescue_from ActiveRecord::RecordNotFound do
flash[:warning] = 'Resource not found.'
redirect_back_or root_path
end
def redirect_back_or(path)
redirect_to request.referer || path
end
Whenever, I would click on the delete button twice in quick succession, the destroy action would be rerouted to the scoreboard#show page and proceed to call the destroy method for that resource on the second click. This is because the #team which the destroy method was being called for had already been destroyed in the first click therefore requesting a redirect. I did get the flash messages after I refreshed the page to go the home page but dismissed them as relevant at first but they were key to the conclusion.
The code infrastructure looks good, I would recommend looking at the associations and making sure you don't have dependent: :destroy on team belongs_to :scoreboard
--
In regards the problem of multiple "delete" button clicking, the problem looks like you're getting a redirect to the parent resource. I don't have any reason why this would be the case, except that perhaps Rails has an inbuilt set of functionality to load "parent" routes if the child fails.
The way I would tackle the fix is to use conditions:
def destroy
#scoreboard = Scoreboard.find(params[:scoreboard_id])
#team = #scoreboard.teams.find params[:id]
if #team.destroy
respond_to do |format|
format.html {redirect_to scoreboard_url(#scoreboard)} #-> could this be the reason for the redirect?????
format.js
end
else
redirect_to scoreboard_teams_path(#scoreboard), notice: "Team Already Deleted"
end
end
I'd also look at conditioning the #team -- if #team && #team.destroy - I can refactor if you wanted more information.
Doing this will give you a definite flow which can handle exceptions. I think the problem is that when you click the delete button (and the record no longer exists), Rails is unable to handle the exception.
The inbuilt way that Rails comes back with errors is to redirect to the object_path(#object), and show the errors (like you've got in your format.html).
Therefore, I would guess that Rails is trying to take you back to #scoreboard (scoreboard_path(#scoreboard)), and since you have method: :delete, it's running the destroy method for that controller.
To fix it, you'd need to use the conditions above to let Rails know what to do in case of problems.
In your teams_controller in the destroy action, I suggest you change this line:
#team = #scoreboard.teams.find(params[:id])
for
#team = Team.find(params[:id])

How to order a collection of a habtm-association by created_at

I have three main Classes: Users, Vocabs and Tags. A User has many Tags. Tags has-and-belongs-to-many Vocabs (and the other way around).
How can I order a collection of Tags when getting them through a Vocab-Object?
#vocab.tag
gets the tags from the joins table, which has no created_at column.
Is there a handy way to solve this problem?
I am pretty new to Rails so excuse me if this is obvious.
EDIT: I just tried
#tags = #vocab.tags.order('tags.created_at DESC')
but without success.
The computer says:
Started GET "/users/1/vocabs/61" for 127.0.0.1 at 2014-12-11 15:41:15 +0100
Processing by VocabsController#show as HTML
Parameters: {"user_id"=>"1", "id"=>"61"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Vocab Load (0.3ms) SELECT "vocabs".* FROM "vocabs" WHERE "vocabs"."user_id" = ? AND "vocabs"."id"
= ? ORDER BY created_at DESC LIMIT 1 [["user_id", 1], ["id", 61]]
Tag Load (0.5ms) SELECT DISTINCT "tags".* FROM "tags" INNER JOIN "tags_vocabs" ON "tags"."id" =
"tags_vocabs"."tag_id" WHERE "tags_vocabs"."vocab_id" = ? [["vocab_id", 61]]
Rendered shared/_error_messages.html.erb (0.2ms)
Rendered vocabs/show.html.erb within layouts/application (10.7ms)
Rendered layouts/_shim.html.erb (0.1ms)
Rendered layouts/_header.html.erb (1.1ms)
Completed 200 OK in 602ms (Views: 587.3ms | ActiveRecord: 1.2ms)
The controller action looks like this:
def new_tag
#user = current_user
#vocab = #user.vocabs.find(params[:id])
#tags = #vocab.tags.order('tags.created_at DESC')
#tag = current_user.tags.build(name: params[:tag])
if #tag.save
#vocab.tags<<#tag
flash.now[:success] ='Tag successfully created.'
redirect_to user_vocab_path(#user, #vocab)
else
flash.now[:danger] = "Tag could not be created!"
render :action => "show"
end
end

Render does not work after call to update function in Rails

My controller's update function looks like this:
def update
#cohort = Cohort.find(params[:cohort][:id])
#cohort.update_attributes(params[:cohort])
respond_to do |format|
format.js {render action: "show", layout: "courses"}
end
end
When I trigger the update function (via js), get the following in my Rails console:
Started PUT "/cohorts/41" for 127.0.0.1 at 2014-07-18 21:57:26 -0400
Processing by CohortsController#update as */*
Parameters: {"cohort"=>{"id"=>"41", "user_id"=>"17", "room_id"=>"16"}, "id"=>"41"}
Cohort Load (0.1ms) SELECT "cohorts".* FROM "cohorts" WHERE "cohorts"."id" = ? LIMIT 1 [["id", "41"]]
(0.0ms) begin transaction
(0.3ms) UPDATE "cohorts" SET "room_id" = 16, "updated_at" = '2014-07-18 21:57:26.303928' WHERE "cohorts"."id" = 41
(8.5ms) commit transaction
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 17 LIMIT 1
Course Load (0.1ms) SELECT "courses".* FROM "courses" WHERE "courses"."id" = 21 LIMIT 1
Timeperiod Load (0.1ms) SELECT "timeperiods".* FROM "timeperiods" WHERE "timeperiods"."id" = 37 LIMIT 1
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 17 LIMIT 1
User Load (0.2ms) SELECT "users".* FROM "users" WHERE (role = 'faculty' OR role = 'admin')
FacultyReport Load (0.1ms) SELECT "faculty_reports".* FROM "faculty_reports" WHERE "faculty_reports"."cohort_id" = 41 LIMIT 1
Room Load (0.1ms) SELECT "rooms".* FROM "rooms"
Room Load (0.1ms) SELECT "rooms".* FROM "rooms" WHERE "rooms"."id" = 16 LIMIT 1
Enrollment Load (0.1ms) SELECT "enrollments".* FROM "enrollments" WHERE "enrollments"."cohort_id" = 41
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 17 LIMIT 1
Rendered cohorts/show.html.erb within layouts/courses (12.3ms)
Rendered layouts/_header.html.erb (2.1ms)
Rendered layouts/_leftnav.html.erb (0.8ms)
Rendered layouts/_messages.html.erb (0.1ms)
Completed 200 OK in 44.7ms (Views: 31.1ms | ActiveRecord: 10.0ms)
Notice it says Rendered cohorts/show.html.erb within layouts/courses (12.3ms). In the browser itself, no refresh or re-rendering occurs. If I manually refresh the page, I can see the changes were made but the page itself change.
Also, if I change render to redirect_to I get (in the console) a redirect loop that goes for about 14 iterations and then stops and no changes in the browser.
respond_to
From your comments & looking at your code, you need to remember how Rails repond_to block works
When you call format.js, Rails will automatically look for [action_name].js.erb in your views/controller folder. This file will essentially run when you hit the update action from ajax / JS, it will perform the function & then look for update.js.erb to run.
You have several issues with your code:
#app/controllers/your_controller.rb
Class YourController < ApplicationController
def update
#cohort = Cohort.find(params[:cohort][:id])
#cohort.update_attributes(params[:cohort])
respond_to do |format|
format.js {render layout: "courses"} #-> automatically loads views/your_controller/update.js.erb
end
end
end
--
Update
Something else you should consider is your update functionality - specifically that you're trying to update a model with naked params (you should use strong_params):
def update
#cohort = Cohort.find params[:cohort][:id]
#cohort.update(cohort_params)
end
private
def cohort_params
params.require(:cohort).permit(:your, :params)
end
Edit:
I changed format.js {render action: "show", layout: "courses"} to format.js {render "update.js.erb"} and it works as intended.

Resources