使用嵌套属性保存数据时出现问题

时间:2011-07-21 15:04:03

标签: ruby-on-rails-3 nested-attributes

我正在使用Ruby On Rails 3.0.7构建一个网站,我想保存一个store对象及其languages。所以,我有以下模型:

class Store < ActiveRecord::Base
  belongs_to :user
  has_many :languages, :through => :store_languages
  has_many :store_languages

  accepts_nested_attributes_for :store_languages

  #Validations
  validates :title, :presence => true, :length => 5..100
  validates :contact_email, :presence => true, :format => { :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i }
end

class Language < ActiveRecord::Base
  has_many :stores, :through => :store_languages
  has_many :store_languages
end

class StoreLanguage < ActiveRecord::Base
  belongs_to :store
  belongs_to :language

  validates :store_id, :presence => true
  validates :language_id, :presence => true
end

StoresController的相关行动:

  def new
    @store = Store.new
    @store.store_languages.build

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @store }
    end
  end

  # POST /stores
  # POST /stores.xml
  def create
    #raise params.inspect
    @store = current_user.stores.new(params[:store])

    respond_to do |format|
      if @store.save
        format.html { redirect_to(@store, :notice => 'Store was successfully created.') }
        format.xml  { render :xml => @store, :status => :created, :location => @store }
      else
        @store.store_languages.build
        format.html { render :action => "new" }
        format.xml  { render :xml => @store.errors, :status => :unprocessable_entity }
      end
    end
  end

查看:/stores/new.html.erb:

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

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

  <p>
    <label for="title">Title*</label><br />
    <%= f.text_field :title %>
  </p>

  <p>
    <label for="description">Description</label><br />
    <%= f.text_field :description %>
  </p>

  <p>
    <label for="contact_email">Contact E-mail*</label><br />
    <%= f.text_field :contact_email %>
  </p>

  <p>
    <label for="logo">Logo</label><br />
    <%= f.file_field :logo %>
  </p>

    <% f.fields_for :store_languages do |lf| %>

      <%= lf.collection_select :language_id, @languages, :id, :name, {}, {:multiple => true } %>
    <% end %>

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

所以,我在languages表中有以下记录:

 id |   name
 3    English
 4    Español

当我创建一个从列表中选择两种语言的新商店时,会在store_languages表中保存以下内容:

id | store_id | LANGUAGE_ID  4 4 1

而且language_id = 1不存在。

如果我在create操作中调试应用程序,我会收到以下信息:

"store"=>{"title"=>"asdasdsdsadasdasdasd", "description"=>"", "contact_email"=>"asdasdsa@asdasdsad.com", "logo"=>"", "store_languages_attributes"=>{"0"=>{"language_id"=>["3", "4"]}}}

你可以看到这里的ID是正确的:3和4.所以,我不知道为什么它会保存1。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

尝试

  <%= lf.collection_select :language_ids, @languages, :id, :name, {}, {:multiple => true } %>

(即在属性名称中使用ids代替id