为什么我仍然收到错误“无法批量分配受保护的属性:联系人”

时间:2012-07-13 17:48:59

标签: ruby-on-rails ruby-on-rails-3 associations nested-forms

当我尝试将留下邮件字段的新女孩记录留空时,我收到此错误 邮件字段是嵌套的。

当它没有输入时,我只想让它在没有创建联系人记录的情况下只制作Girl记录 当它充满了某些东西时,我希望它能同时创造女孩和接触记录 当它更新时,我希望它做同样的事情。
我怎样才能做到这一点???我的代码出了什么问题?

我的观点

<%= form_for(@girl) do |f| %>
  <% if @girl.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@girl.errors.count, "error") %> prohibited this girl from being saved:</h2>

      <ul>
      <% @girl.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name_en %><br />
    <%= f.text_field :name_en %>
  </div>
  <div class="field">
    <%= f.label :name_ja %><br />
    <%= f.text_field :name_ja %>
  </div>
  <div class="field">
    <%= f.label :gender_id %><br />
    <%= f.number_field :gender_id %>
  </div>
  <div class="field">
    <%= f.label :job_type_id %><br />
    <%= f.number_field :job_type_id %>
  </div>
  <div class="field">
    <%= f.label :age %><br />
    <%= f.number_field :age %>
  </div>
  <div class="field">
    <%= f.fields_for :contacts do |contact| %>
    <%= f.label :mail %><br />
    <%= contact.text_field :mail %>
    <% end %>
  </div>

  <div class="field">
    <%= f.label :photo %><br />
    <%= f.file_field :photo %>
  </div>

    <div class="field">
      <%= f.label :tag_list, 'tag' %><br />
      <%= f.text_field :tag_list %>
    </div> 

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

我的控制器

  def new

    @girl = Girl.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @girl }
    end
  end


  def create

    @girl = Girl.new(params[:girl])

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


  def update

    @girl = Girl.find(params[:id])

    respond_to do |format|
      if @girl.update_attributes(params[:girl])
        format.html { redirect_to @girl, notice: 'Girl was successfully updated.' }
        format.json { head :ok }
      else
        format.html { render action: "edit" }
        format.json { render json: @girl.errors, status: :unprocessable_entity }
      end
    end
  end

女模特

class Girl < ActiveRecord::Base
  has_many :users
  has_one :contact
  accepts_nested_attributes_for :contact
  attr_accessible :id, :name_en, :name_ja, :gender_id, :job_type_id, :age, :contact_attributes, :photo, :tag_list

  searchable do 
    text :name_en, :name_ja
    text :contact do 
      contact.mail 
    end 
  end

    has_attached_file :photo,
     :styles => {
       :thumb=> "100x100>",
       :small  => "400x400>" } 

  acts_as_taggable_on :tags
  acts_as_commentable


end

联系模式

class Contact < ActiveRecord::Base
  belongs_to :girl, :class_name => "Girl"
  accepts_nested_attributes_for :girl
  attr_accessible :mail

end

1 个答案:

答案 0 :(得分:1)

这是一个问题,因为他们修补了最新版本的rails。您可以在配置中更改它。有关详细信息,请参阅http://weblog.rubyonrails.org/2012/3/30/ann-rails-3-2-3-has-been-released/

当你的形式应该使用单数时,看起来你正在使用复数。

 <div class="field">
    <%= f.fields_for :contacts do |contact| %>
    <%= f.label :mail %><br />
    <%= contact.text_field :mail %>
    <% end %>
 </div>

应该是:

 <div class="field">
    <%= f.fields_for :contact do |contact| %>
    <%= f.label :mail %><br />
    <%= contact.text_field :mail %>
    <% end %>
 </div>

您需要将以下内容添加到控制器中以执行新操作:

@girl.build_contact