1
我在遵循rails教程時遇到了錯誤。經過一番研究,將@post = Post.new(params[:post])
更改爲@post = Post.new(post_params)
解決了我的問題,雖然我還是不太明白原因。是什麼造成了差異?爲什麼我可以在show
中使用params[:id]
?ActiveModel :: ForbiddenAttributesError與Rails 4
我posts_controller.rb:
class PostsController < ApplicationController
def new
end
def create
@post = Post.new(post_params)
@post.save
redirect_to @post
end
def show
@post = Post.find(params[:id])
end
private
def post_params
params.require(:post).permit(:title, :text)
end
end
查看本教程。你得到了答案:http://guides.rubyonrails.org/action_controller_overview.html#strong-parameters – Emu
檢查[欄4:強參數](http://weblog.rubyonrails.org/2012/3/21/strong-參數/).. –
檢查http://ruby.railstutorial.org/chapters/sign-up#sec-strong_parameters – Monideep