ruby on rails web app controller生成错误

时间:2014-06-03 01:52:07

标签: ruby-on-rails ruby ruby-on-rails-4 ruby-on-rails-3.2

我正在关注视频系列,其中作者正在使用RoR 4创建一个简单的CMS,但我目前安装了RoR 3.x.我将所有代码都跟进了一个" T"但是当我尝试在简单的CMS中创建一个简单的主题时,我收到了一个错误。所以我检查了log / production.log

我收到以下错误, 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

我为主题新参数创建的私有方法如下所示

 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

1 个答案:

答案 0 :(得分:3)

作者告诉您,您需要将@subject = Subject.new(subject_params)更改为@subject = Subject.new(params[:subject])

params.require(:subject).permit(:name, :position, :visible)是一个名为Strong Parameters的Rails 4功能。如果您是Rails的新手,我建议您使用作者用来缓解此类未来版本问题的版本。