简体   繁体   English

Ruby on Rails Web应用程序控制器生成错误

[英]ruby on rails web app controller generates error

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. 我正在观看一个视频系列,作者在其中使用RoR 4创建了一个简单的CMS,但是我目前安装了RoR3.x。 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. 我将所有代码都跟在了“ T”后面,但是当我尝试在简单的CMS中创建一个简单的主题时,出现了错误。 So I checked the log/production.log 所以我检查了log / production.log

And I am getting the following error, TypeError (no implicit conversion of Symbol into String): 我收到以下错误, 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]) . 作者告诉您,您需要将@subject = Subject.new(subject_params)更改为@subject = Subject.new(params[:subject])

params.require(:subject).permit(:name, :position, :visible) is a Rails 4 feature called Strong Parameters . params.require(:subject).permit(:name, :position, :visible)是Rails 4的一个功能,称为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. 如果您不熟悉Rails,建议您使用作者使用的版本来缓解此类将来的版本问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM