Try the following i hope it will help you.
def update
@post = Post.find(params[:id])
if @post.update(post_params)
redirect_to @post
else
render 'edit'
end
end
题
I receive an error when trying to edit a post:
posts#edit controller:
def edit
@post = Post.find params[:id]
end
posts#update controller:
def update
@post = Post.find params[:post]
if @post.update_attributes params[:post]
redirect_to posts_path
else
render 'edit'
end
end
edit view:
h1 Edit Post
= form_for @post do |f|
p
= f.label :title
= f.text_field :title
p
br
p
= f.label :content
= f.text_area :content
p
br
p
= f.submit 'Update Post'
p
br
This is when I get the ArgumentError in PostsController#update, Unknown Key: title error. I am still wrapping my head around the strong parameter concept in Rails 4 so it might have something to do with this...any ideas?
解决方案
Try the following i hope it will help you.
def update
@post = Post.find(params[:id])
if @post.update(post_params)
redirect_to @post
else
render 'edit'
end
end
其他提示
I updated the posts#update controller to
params[:id]
rather than
params[:post]
this fix brought me the ForbiddenAttributesError caused by
if @post.update_attributes params[:post]
this is due to the strong parameters concept introduced with Rails 4...I fixed this by substituting the
params[:post]
with a strong parameter
post_params
from the private method post_params
private
def post_params
params.require(:post).permit(:title, :content)
end
eventually solving the unknown key error of :title