开发者

Saving all nested form objects in one go

开发者 https://www.devze.com 2023-02-09 01:44 出处:网络
I\'m trying to build two forms, nested. But all the resources I\'ve found so far deal with how to save the main object, maybe add or remove nested objects, but not saving the entire list elsewhere wit

I'm trying to build two forms, nested. But all the resources I've found so far deal with how to save the main object, maybe add or remove nested objects, but not saving the entire list elsewhere with the values of the form. Is there a way to do this?

Here're the model I'm working with. I'm trying to create a pc configurator which will allow you to choose different components and quantities, and then you would add to the basket.

So I'm working with three models. Configuration, Configoption, Item.

-- Configuration model

  has_many :configoptions

-- Configoption model

  belongs_to :configuration
  has_many :items

  def range
    a = []
    b = self.quantity_rng.scan(/\w/)
    b.each do |p|
      a << p.to_i 
    end
    return a
  end

-- Item model

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

The idea is that every configuration has a set of options, say 5. For each of these 5 options there could be a number of available items. For example option 1 is the processor, 2 could be RAM and so on.

-- Configuration controller

  def list
    @configurations = Configuration.find(:all)
  end

  def show
    @configuration = Configuration.find(params[:id])
    @options = @configuration.configoptions
  end

-- configuration/show view

<table>
 <tr>
    <th>Elements</th>
    <th>Quantity</th>
 </tr>

    <%= form_for(:configuration, :remote => true)  do |p| %>
        <% @options.each do |option| %>
        <tr>
        <%= form_for(:option, :remote => true)  do |f| %>
            <% if option.items.count > 1 %>
            <th id="<%= option.name %>"> <%= f.select option.name, options_from_collection_for_select(option.items.sorted, 'id', 'name')%></th>
 开发者_StackOverflow中文版           <% else%>
            <th id="<%= option.name %>"> <%= f.label(option.items[0].name) %></th>
            <% end %>
            <% if option.quantity_rng == nil  %>
            <th id="<%= option.name + "_qty" %>"> <%= f.label(option.quantity) %></th>
            <% else%>
            <th id="<%= option.name %>"> <%= f.select option.name, option.range, :selected => option.quantity%></th>
            <% end %>
        <% end %>
        <% end %>
    </tr>
    <% end %>

</table>

So far things are actually good, I can give different items to the options and the .range method let's me say quantity_rng in an option "1,2,3" and will make it into the array [1,2,3] passed for a drop-down if needed.

But now comes the crucial part, actually adding what I selected to the basket. How do I capture what the user has manipulated in the drop downs and store it (not as changes to the configoptions themselves, but as a new object elsewhere?)

BTW I'm using remote => true, because I intend to put some validation rules from jQuery later on, but one step at a time ;)

Thanks a lot!


May be i dint understand well your idea but i thing you need to use p.fields_for as nested options. Look at http://apidock.com/rails/v3.0.0/ActionView/Helpers/FormHelper/fields_for and dot forget to define

def address_attributes=(attributes)
  # Process the attributes hash
end

in your model Configuration

0

精彩评论

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