Reputation: 365
In a Rails app I have Users who can have notes created under them in each user show view.
From the show view notes can be added and edited. The edit link routes properly to the edit path for each note. Clicking save to update the note redirects back to the user show view.
Here is my notes controller:
def update
@note = Note.find(params[:id])
redirect_to user_path(@note.user)
end
However, I am trying to update a note entry and in the console I am seeing that it is not updating for some reason. There should be UPDATE step between BEGIN and COMMIT, but it seems to be missing here.
Started PUT "/notes/2" for 127.0.0.1 at 2013-02-01 01:50:25 -0800
Processing by NotesController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Wr+urG+6QvsbPuFpVGUEEqc8QVYiu5q8j389YmOi6Zg=", "note"=>{"author"=>"MZ", "note"=>"Test"}, "id"=>"2"}
Note Load (0.2ms) SELECT "notes".* FROM "notes" WHERE "notes"."id" = $1 LIMIT 1 [["id", "2"]]
(0.2ms) BEGIN
(0.1ms) COMMIT
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Redirected to http://localhost:3000/users/1
Completed 302 Found in 37ms (ActiveRecord: 3.8ms)
What is the reason for why there is no UPDATE step?
Upvotes: 0
Views: 235
Reputation: 10769
You are not updating the attributes. You must call update_attributes
and pass the params
to be updated.
def update
@note = Note.find(params[:id]) #find the note
if @note.update_attributes(params[:note]) #update the note
redirect_to @note.user #if attributes updated redirect to @note.user
else
render :edit #if not, render the form
end
end
Upvotes: 2
Reputation: 2273
Try this, You missing the update_attributes. This is the proper way to call the update method. You will get a flash message if the update was successful.
def update
@note = Note.find(params[:id])
respond_to do |format|
if @note.update_attributes(params[:note]) # you need to make sure about the :note
format.html { redirect_to user_path(@note.user), notice: 'Notes was successfully updated.' }
else
format.html { render actino: "edit" }
end
end
end
Upvotes: 0