开发者

options within nested form

开发者 https://www.devze.com 2023-02-09 11:07 出处:网络
Trying to make a nested form, which is working fine so far, except i need to put some dropdowns for the user to choose, as well as maybe make a couple of validations, however it seems nothing gets out

Trying to make a nested form, which is working fine so far, except i need to put some dropdowns for the user to choose, as well as maybe make a couple of validations, however it seems nothing gets out of the form properly and keep getting errors no matter what I try.

three models. --configuration

  has_many :configoptions
  accepts_nested_attributes_for :configoptions

--configoption

  belongs_to :configuration
  has_many :items

and item

  belongs_to :configoption
  scope :sorted, order('items.position ASC')

Now, so far I'm creating a nested form, looping through the configoptions, but for each option is possible there's more t开发者_C百科han one item. So I want to make a drop-down for those options where this is the case.

In my view i have:

<p>
<th>Elements</th>
<th>Quantity</th>
</p>
<%= form_for @config, :url => {:action => 'show', :id => @config.id} do |f| %>
  <%= f.fields_for :configoptions do |fp| %>
    <p>
        <% if :items.count > 1 %>
        <%= fp.text_field :name %>  
        <% else %>
        <% fp.select(:items, :name)%>
        <% end %>
    <%= fp.text_field :quantity %>
    </p>
  <% end %>    
  <%= f.submit %>
<% end %>

I get an error obviously telling me that it can't count the :items.

How do you think I can make this work?

Thanks!

<%= form_for @config, :url => {:action => 'show', :id => @config.id} do |f| %> <%= f.fields_for :configoptions do |fp| %>

<%= fp.text_field :id %> <%= fp.text_field :name %> <%= fp.text_field :quantity %>

<% end %>

<%= f.submit %> <% end %>


OK, I think I figured it out, at least it seems to be doing what I want now.

I modified the view to pass the instance of the configoption into the nested form itself to be able to create the drop downs.

  <% for configoption in @config.configoptions %>    
  <%= f.fields_for :configoptions, configoption do |fp| %>
    <p>
        <% if configoption.items.count > 1 %>
        <%= fp.select (:name, options_from_collection_for_select(configoption.items.sorted, 'name', 'name'))%>  
        <% else %>
0

精彩评论

暂无评论...
验证码 换一张
取 消