Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer peopleโ€™s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer peopleโ€™s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8787691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:57:08+00:00 2026-06-13T21:57:08+00:00

How to create form and action for multiple nested attributes if: LineItem: has_many :item_options,

  • 0

How to create form and action for multiple nested attributes if:

LineItem:

has_many :item_options, :dependent => :destroy
has_many :product_options, :through => :item_options

ProductOption:

belongs_to :product
belongs_to :option
has_many :item_options
has_many :line_items, :through => :item_options

ItemOption:

attr_accessible :line_item_id, :product_option_id  
belongs_to :line_item, :foreign_key => "line_item_id"
belongs_to :product_option,:foreign_key => "product_option_id"

When I’m creating new LineItem, I need to create new ItemOption(s). This is my form:

        <%= form_for(LineItem.new) do |f| %>
        <%= f.hidden_field :product_id, value: @product.id %>
        <%= f.fields_for :item_options do |io| %>
            <% @product.options.uniq.each do |o| %>
              <%= o.name %>: 
              <%= io.collection_select :product_option_id, o.product_options.where(:product_id => @product.id), :id, :value %>
            <% end %>
        <%= f.submit %>
        <% end %>

When I’m clicking on Add To Cart, I’ve get:

ItemOption(#70296453751440) expected, got Array(#70296430421140)

When Adding accepts_nested_attributes_for :item_options to LineItem, my selects not diplayed ๐Ÿ™

With

<%= select_tag "product_option_id", options_from_collection_for_select(o.product_options.where(:product_id => @product.id), :id, :value) %>
#item_options not created:
Parameters: {"utf8"=>"โœ“", "authenticity_token"=>"/WM5/MqPn1yCxjKWoJQmjfko2pR4RiYV0S2KeTTpA3w=", "line_item"=>{"product_id"=>"1"}, "product_option_id"=>"5", "commit"=>"add"}

And last one, I’ve create action like this:

@line_item = LineItem.new(params[:line_item])
@line_item.item_options.build 
....

Where am I wrong? ๐Ÿ™ I’m totally confused.
ps. similar question Rails 3.2 has_many through form submission
This is form:

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T21:57:09+00:00Added an answer on June 13, 2026 at 9:57 pm

    Looks this line:

    Parameters: {"utf8"=>"โœ“", "authenticity_token"=>"/WM5/MqPn1yCxjKWoJQmjfko2pR4RiYV0S2KeTTpA3w=", "line_item"=>{"product_id"=>"1"}, "product_option_id"=>"5", "commit"=>"add"}
    

    The parameter product_option_id is outside line_item hash, and will be inside. Maybe you need write the select like this:

    <%= select_tag "line_item[product_option_id]", options_from_collection_for_select(o.product_options.where(:product_id => @product.id), :id, :value) %>
    

    I’m not sure, but maybe is this. Maybe I need more information, like the exact line where is failing.


    Extra, the :foreign_key => "line_item_id" and :foreign_key => "product_option_id" are not necesary, because, the belongs_to model name is the same and will use these foreign_key. From api.

    Specify the foreign key used for the association. By default this is
    guessed to be the name of the association with an โ€œ_idโ€ suffix. So
    a class that defines a **belongs_to :person** association will use
    โ€œperson_idโ€ as the default :foreign_key. Similarly, belongs_to
    :favorite_person, :class_name => “Person”
    will use a foreign key of
    โ€œfavorite_person_idโ€.


    Edit

    Sorry, the unknown attribute: product_option_id is because the attribute name is product_option_ids, and is an array, not a unique value. For a has_many relationship, the column name is collection_singular_ids, and the select should be:

    <%= select_tag "line_item[product_option_ids][]", options_from_collection_for_select(o.product_options.where(:product_id => @product.id), :id, :value) %>
    

    This should work, I think :)…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a nested form, structured: Question has_many Answers. So, the questions are multiple
I've create a form like: function create_custom_form($form, &$form_state) { $form['#action'] = #; .... }
I have a create action for a form that potentially generates errors (i.e. first
I have a create action in the controller that I use for a form
I have done the following form <% form_for @anexo, :url => {:action => create},
I have a Form element: $Form=new Zend_Form; $Form->setAction($this->view->url(array('controller'=>'auth','action'=>'create'),null,true)) ->setMethod('post') ->setAttrib('id','auth-form') ->removeAttrib('enctype'); As can be
I am trying to create a form that allows to assign multiple projectmembers to
I created a login page using spring_security_check. Here: <form name='f' action=/sec_test/j_spring_security_check method='POST'> <table> <tr>
I create form dynamically on the client side. I want to iterate it, but
In PHP you can create form elements with names like: category[1] category[2] or even

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.