I am following along a video series, where the author is creating a simple CMS using RoR 4, but I currently have RoR 3.x installed. I followed all the code to a "T" but when I try to create a simple subject in the simple CMS, I am getting an error. So I checked the log/production.log
And I am getting the following error, TypeError (no implicit conversion of Symbol into String):
The controller code looks like the following,
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
And the private method I created for the subject new params looks like the following
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
The author tells you that you need to change @subject = Subject.new(subject_params)
to @subject = Subject.new(params[:subject])
.
params.require(:subject).permit(:name, :position, :visible)
is a Rails 4 feature called Strong Parameters . If you are new to Rails I would suggest you use the version the author uses to alleviate future version issues like this.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.