ruby on rails web应用程序控制器生成错误
-
20-12-2019 - |
题
我正在跟随一个视频系列,其中作者正在使用RoR4创建一个简单的CMS,但我目前有RoR3。安装了x。我遵循了所有的代码到一个"T",但是当我尝试在简单的CMS中创建一个简单的主题时,我得到了一个错误。所以我检查了日志/生产。日志
我得到以下错误,
TypeError (no implicit conversion of Symbol into String):
控制器代码如下所示,
def create
# Instantiate a new object using form parameters
# the below line should work with rails v3.x
# @subject = Subject.new(params[:subject])
@subject = Subject.new(subject_params)
# Save the object
if @subject.save
# add flash hash
flash[:notice] = "Subject created successfully."
# if save succeeds, redirect to the index action
redirect_to(:action => 'index')
else
# if save fails, redisplay the form so user can fix problems
render('new')
end
end
我为subject new params创建的私有方法如下所示
private
def subject_params
# same as using "params[:subject]", except that it:
# - raises an error if :subject is not present
# - allows listed attributes to be mass-assigned
params.require(:subject).permit(:name, :position, :visible)
end
解决方案
作者告诉你,你需要改变 @subject = Subject.new(subject_params)
到 @subject = Subject.new(params[:subject])
.
params.require(:subject).permit(:name, :position, :visible)
是一个Rails4功能称为 强参数.如果你是Rails的新手,我会建议你使用作者使用的版本来缓解像这样的未来版本问题。
不隶属于 StackOverflow