Ruby-on-Rails [SOLVED]: Nested forms: How to correctly access a certain instance with fields_for

Ruby-on-Rails [SOLVED]: Nested forms: How to correctly access a certain instance with fields_for

Home Forums Frameworks Ruby on Rails Tutorials Ruby-on-Rails [SOLVED]: Nested forms: How to correctly access a certain instance with fields_for

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #246762

    Cloudy Point
    Keymaster

    QuestionQuestion

    I have an Order, which always has exactly two addresses: A invoice address and a shipping address. In the view, I have a form, where you can enter both addresses. From my controller I pass on the instance @order, with two sub instances of addresses already built.

    Extract from my OrdersConntroller:

    def new
      @order = Order.new(orderCreationDate: Time.now)
      2.times{ @order.addresses.build }
    end
    

    In the view, how can I access just the first @order.addresses, and then in some other place the second @order.addresses?

    What I’ve tried so far:

    <%= form_for @order do |f| %>
    ...
      <%= f.fields_for :addresses do |ff| %>
       <%= ff.text_field :firstName %> 
      ...
      <% end %>
    <% end %>
    

    But then the HTML result is the following (where the name and id is not right).

    <input type="text" name="order[a][firstName]" id="order_a_firstName">      
    

    Extract from my Order model:

    has_many :addresses, foreign_key: "orderID"
    accepts_nested_attributes_for :addresses, allow_destroy: true
    

    Thx for the support.

    #246763

    Cloudy Point
    Keymaster

    Accepted AnswerAnswer

    You need to create two model 1) InvoiceAddress and 2) ShippingAddress

    class InvoiceAddress < ActiveRecord::Base
       self.table_name = :address
    end
    
    class ShippingAddress < ActiveRecord::Base
       self.table_name = :address
    end
    

    and in order model use association as follows

    has_one :invoice_address
    has_one :shipping_address
    

    and orders_controller.rb

    def new
      @order = Order.new(orderCreationDate: Time.now)
      @invoice_address = @order.build_invoice_address
      @shipping_address = @order.build_shipping_address
    end
    

    form will be

    <%= form_for @order do |f| %>
       ...
       <%= f.fields_for :invoice_address, @invoice_address  do |ff| %>
          <%= ff.text_field :firstName %> 
          ...
       <% end %>
       <%= f.fields_for :shipping_address, @shipping_address  do |ff| %>
          <%= ff.text_field :firstName %> 
          ...
       <% end %>
    <% end %>
    

    Source: https://stackoverflow.com/questions/47966762/nested-forms-how-to-correctly-access-a-certain-instance-with-fields-for
    Author: Ganesh Navale
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.