Rails控制器:更新操作无法执行SQL更新?
在Rails应用程序中,我有一些用户可以在每个用户显示视图中创建注释 从“显示”视图中,可以添加和编辑注释。“编辑”链接将正确路由到每个注释的编辑路径。单击“保存”更新便笺将重定向回“用户显示”视图 这是我的notes控制器:Rails控制器:更新操作无法执行SQL更新?,sql,ruby-on-rails,controller,Sql,Ruby On Rails,Controller,在Rails应用程序中,我有一些用户可以在每个用户显示视图中创建注释 从“显示”视图中,可以添加和编辑注释。“编辑”链接将正确路由到每个注释的编辑路径。单击“保存”更新便笺将重定向回“用户显示”视图 这是我的notes控制器: def update @note = Note.find(params[:id]) redirect_to user_path(@note.user) end 但是,我正在尝试更新一个注释条目,在控制台中,我看到它由于某种原因没有更新。在BEG
def update
@note = Note.find(params[:id])
redirect_to user_path(@note.user)
end
但是,我正在尝试更新一个注释条目,在控制台中,我看到它由于某种原因没有更新。在BEGIN和COMMIT之间应该有一个更新步骤,但这里似乎没有
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)
没有更新步骤的原因是什么?您没有更新属性。您必须调用
update\u attributes
并传递要更新的参数
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
尝试此操作,您将丢失update_属性。这是调用update方法的正确方法。如果更新成功,您将收到一条闪光消息
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
是的。。。很抱歉拼写错误,我没有在问题本身上找到任何udpate活动记录,即@note.update_属性(params[:note])