I am a rails beginner,now I follow the guide and create a test project as below:
admin_controller.rb
def blogEdit
@btnName = "更新"
@submitURL = "blogUpdate"
@blog=Blog.find(params[:id])
end
def blogUpdate
@blog=Blog.find(params[:id])
@blog.update(params[:blog])
redirect_to "/admin/blogList"
end
blogEdit.slim
= render "blogForm"
_blogForm.slim
=form_for :blog,url:{action: @submitURL} do |f|
=f.label :title ,"标题"
=f.text_field :title
=f.label :body ,"内容"
=f.text_area :body,rows:10,style:'width:500px;'
=f.label :author ,"作者"
=f.text_field :author
=f.hidden_field :id
=f.submit @btnName,class:'btn'
but when I update a blog,I get the error:
Couldn't find Blog without an ID
Rails.root: /home/hxh/share/ruby/myblog
Application Trace | Framework Trace | Full Trace
app/controllers/admin_controller.rb:28:in `blogUpdate'
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"3SBp+xSft8SzA5poczxNSU1zKReO2OpZQqgESn6ZURs=",
"blog"=>{"title"=>"违法的",
"body"=>"的说法都是",
"author"=>"似懂非懂",
"id"=>"2"},
"commit"=>"更新"}
so It shows that I not get the params[:id]
@blog=Blog.find(params[:id])
I had follow the guide,why I got the result?
You're passing =f.hidden_field :id
into form. So your id
in in params[:blog][:id]
. But id
have to be passing via @submitURL
. Judging by your params @submitUrl
aren't contains id
of the blog.
You are not following guide convensions or your guide doesn't do it. Because there is common rules for name actions. It's edit
and update
instead your blogEdit
and blogUpdate
.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다