Rails想要保存attr_accessor属性

时间:2012-06-07 09:59:03

标签: ruby-on-rails-3

在我的模型中有一些自定义的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创建这个输入,就像其他'真实'字段一样,这就是我使用'非真实属性'的原因......

感谢,

编辑:

在此处将params分配给模型属性之前,保存时不会出现错误:

    @expedient=Expedient.new.attributes=params[:expedient]

1 个答案:

答案 0 :(得分:0)

使entity_selectoffice_select属性可访问:

class Expedient < ActiveRecord::Base
  # Add:
  attr_accessible :entity_select, :office_select