在我的模型中有一些自定义的 attr_accessor。
当我尝试使用 params[:model] 创建新模型时,出现此错误:
ActiveModel::MassAssignmentSecurity::Error (Can't mass-assign protected attributes: entity_select, office_select):
class Expedient < ActiveRecord::Base
belongs_to :enterprise
has_many :document
attr_accessor :entity_select
attr_accessor :office_select
...
我想 Rails 知道那些是 att_accessor 并且不应该保存在数据库中的模型上。
或不 ?
我正在使用这个 attr_accessor 创建一些使用 simple_form 的帮助字段:
<%= f.input :entity_select,:label => 'Entity', :input_html => {:class => "span2"}, :wrapper => :prepend do %>
<span class="add-on"><i class="icon-search"></i></span><%= f.input_field :entity_select, :class => "span2 typeahead_entity", :id_selected => '99', :id => 'typeahead_centre'%>
<%end%>
我需要一个特殊的文本输入来输入类型,但我想使用 simple_form 创建这个输入,就像其他“真实”字段一样,这就是我使用“非真实属性”的原因......
谢谢,
编辑:
错误不是在保存之前,在此处将参数分配给模型属性时:
@expedient=Expedient.new.attributes=params[:expedient]