Problem saving data using Nested Attributes
I'm
I'm building a website on Ruby On Rails 3.0.7 and I want to save a store
object and its languages
. So, I have the following models:
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's relevant actions:
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
View: /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 %>
So, I've got the following records in the languages
table:
id | name
3 English
4 Español
What happens is that when I create a new store selecting the two languages from the list, it will save the following at the store_languages
table:
id | store_id | language_id 4 4 1
And the language_id = 1 doesn't exist.
If I debug the application at the create
action, I get the following:
"store"=>{"title"=>"asdasdsdsadasdasdasd", "description"=>"", "contact_email"=>"asdasdsa@asdasdsad.com", "logo"=>"", "store_languages_attributes"=>{"0"=>{"language_id"=>["3", "4"]}}}
You can see that the ids are correct here: 3 and 4. So, I don't know why it saves 1.
Any ideas?
Try
<%= lf.collection_select :language_ids, @languages, :id, :name, {}, {:multiple => true } %>
(ie using ids
instead of id
in the attribute name)
精彩评论