ruby on rails 3 - Problem saving data using Nested Attributes -
i'm
i'm building website on ruby on rails 3.0.7 , want save store
object , languages
. so, have 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 |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 |format| if @store.save format.html { redirect_to(@store, :notice => 'store 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) |f| %> <% if @store.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@store.errors.count, "error") %> prohibited store being saved:</h2> <ul> <% @store.errors.full_messages.each |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 |lf| %> <%= lf.collection_select :language_id, @languages, :id, :name, {}, {:multiple => true } %> <% end %> <div class="actions"> <%= f.submit %> </div> <% end %>
so, i've got following records in languages
table:
id | name 3 english 4 espaƱol
what happens when create new store selecting 2 languages list, save following @ store_languages
table:
id | store_id | language_id 4 4 1
and language_id = 1 doesn't exist.
if debug application @ create
action, following:
"store"=>{"title"=>"asdasdsdsadasdasdasd", "description"=>"", "contact_email"=>"asdasdsa@asdasdsad.com", "logo"=>"", "store_languages_attributes"=>{"0"=>{"language_id"=>["3", "4"]}}}
you can see ids correct here: 3 , 4. so, don't know why saves 1.
any ideas?
try
<%= lf.collection_select :language_ids, @languages, :id, :name, {}, {:multiple => true } %>
(ie using ids
instead of id
in attribute name)
Comments
Post a Comment