【问题标题】:Upgrade to strong params causing create action to throw mass assignment error升级到强参数导致创建操作抛出质量分配错误
【发布时间】:2013-07-22 13:35:04
【问题描述】:

我正在按照 RailsCast #415 (http://railscasts.com/episodes/415-upgrading-to-rails-4?view=asciicast) 将我的 Rails 应用程序从 Rails 3.2.8 升级到 Rails 4.0.0,在升级到强参数之前一切都很好。在更新事件控制​​器以使用强参数后,我可以提交一个表单来创建一个新事件(不会引发验证错误),但所有提交的参数都是空的,并且终端日志(在本地运行)说,

WARNING: Can't mass-assign protected attributes for Event: street, description, host_name, event_date(1i), event_date(2i), event_date(3i), event_time(1i), event_time(2i), event_time(3i), event_time(4i), event_time(5i), event_name, end_time(1i), end_time(2i), end_time(3i), end_time(4i), end_time(5i)

很明显我遇到了质量分配问题,并且强参数无法正常工作。

Events 控制器有以下私有方法:

private
  def event_params
    params.require(:event).permit(:city, :state, :street, :zip, :description,
        :host_name, :host_contact, :event_date, :event_time,
        :instructions, :event_name, :end_time)
  end

并且 event#create 动作看起来像(使用脚手架创建):

  def create
    @event = Event.new(event_params)

    respond_to do |format|
      if @event.save
        format.html { redirect_to @event, notice: 'Event was successfully created.' }
        format.json { render json: @event, status: :created, location: @event }
      else
        format.html { render action: "new" }
        format.json { render json: @event.errors, status: :unprocessable_entity }
      end
    end
  end

gemfile 包含以下 gem,可在转换期间使用: gem 'protected_attributes'

application.rb 文件的 whitelist_attributes 设置为 false(已被注释掉):

# config.active_record.whitelist_attributes = true

我在此处连接点时缺少的任何帮助都会有所帮助。谢谢。

【问题讨论】:

  • 在我的 gemfile 中有“gem 'protected_attributes'”会导致与重新定义的方法发生冲突。删除它,一切都很好。

标签: ruby-on-rails-4


【解决方案1】:

在我的 gemfile 中有“gem 'protected_attributes'”会导致与重新定义的方法发生冲突。删除它,一切都很好。

【讨论】:

  • 刚刚遇到了同样的问题。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-21
  • 2014-07-02
  • 2018-02-07
相关资源
最近更新 更多