简体   繁体   中英

Nested form with collection_select and multiple selection

Sorry for long post, usually don't post here often.

Rails 3.1

I have a Company model. Company has many custom properties. All properties names are standardized (eg company size, company revenue, etc). Some property values are custom and some are "standartized" (relation to another table CompanyProperty - property_value_id).

What I am trying to do is a form where new Company Properties are added. In a form I try to add different properties at once. Everything works except I cannot figure out how to add multiple values via collection_select.

Here are models:

Company:
   has_many :properties, :class_name => 'CompanyProperty'

CompanyProperty:
   belongs_to :company
   belongs_to :property, :class_name => 'Property'
   belongs_to :property_value, :class_name => 'PropertyValue', :foreign_key => 'properties_value_id'


Property:
  has_many :company_properties
  has_many :property_values

PropertyValue:
  belongs_to :properties
  has_many :company_properties

Form (marked comment # problem where using collection_select with multiple selection):

<%= form_for(@company, :url => {:action => 'update_company', :id => @company.id}, :html => {:multipart => true}) do |f| %>
   <% @company.properties.each.with_index do |p,i| %>

        <%= f.fields_for :properties, p do |builder| %>

        <%= p.property.title %><br />

        <% if p.property.standard == 0 %>
            <%= builder.hidden_field :property_id %>
            <%= builder.text_field :value %> <br />

        <% elsif p.property.standard == 1 %>
            <%= builder.hidden_field :property_id %>
            <%= builder.collection_select(:properties_value_id, p.property.property_values, :id, :text_value, {:include_blank => true},
                                  {:class => 'form-control'}) %>
        # Problem:
        <% elsif p.property.standard == 2 %>
                    <%= builder.hidden_field :property_id %>

                    <%= builder.collection_select(:properties_value_id, p.property.property_values, :id, :text_value, {:include_blank => false},
                                  {:multiple => true, :size => p.property.property_values.count, :class => 'form-control'}) %>
        <% end %>

        <% end %>
      <% end %>



    <%= submit_tag("Save", :class => "btn btn-primary btn-block") %>
<% end %>

Controller:

  def update_company
    @company = Company.find(params[:id])
    if @company.update_attributes(params[:company])
      render :text => "Saved"
    else
      error = @company.errors.full_messages.map{|o| "<li>" + o + "</li>" }.join("") + "</ul>"
      render :text => error
    end
  end

All property_id and values are saved, except values from collection_select with multiple selection. The post parameters goes something like this (4 records created, expected result - 6 new records):

  Parameters: {"company"=>{"properties_attributes"=>{"0"=>{"property_id"=>"1", "properties_value_id"=>"2"}, "1"=>{"property_id"=>"2", "value"=>"34"},

 "2"=>{"property_id"=>"3", "properties_value_id"=>["", "4", "5", "6"]}, 

"3"=>{"property_id"=>"4", "value"=>"34"}}}, "commit"=>"Save", "id"=>"16"}

property_id=>3 is that particular property that I'm trying to save. Expected result is to see three new records on CompanyProperties table with property_id = 3 and values properties_value_id accordingly. But this does not happens. Only one records is being created with property_id=3 and the properties_value_id is 1 (why?, there is no such value).

Plus I cannot understand why there is blank param here "properties_value_id"=>["", "4", "5", "6"]}. There are no such value :/

Could any one help to reach my expected result?

This should be a comment, but I'll post here to keep it readable:


The blank param will likely be a "selector" / "default" value in your select box. Failing that, is there any way you can see how your HTML element may be including a separate element in the property_value_ids ?

If you were using Rails 4, I'd recommend your strong params were incorrect - however, as you're using Rails 3, I'd surmise you should do something like this:

<%= builder.collection_select("properties_value_id[]", p.property.property_values, :id, :text_value, {:include_blank => false},
                                  {:multiple => true, :size => p.property.property_values.count, :class => 'form-control'}) %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM