0

I need to create two similar object in one form at one time. But this objects (aviaslers) should be framed with different html. Objects are aviasaler_there(aviasaker who sells ticket in one side) and aviasaler_back(aviasaker who sells ticket in back side) I'm trying to find out how to generate two objects instead of creating to similar models.

My attempt looks like this code. But it doesn't works. Please, help me find out how to reasize this approach and don't make two similar models.

trips_controller.rb

class TripsController < ApplicationController

def new
 @trip = Trip.new
 aviasaler_there = @trip.aviasalers.build
 avisaler_back = @trip.aviasalers.build
end

def create
 @trip = current_user.trips.build(params[:trip])
 if @trip.save
   flash[:success] = "Trip created!"
   redirect_to root_url
 else
   render 'new'
  end
 end
end

new.html.erb

<% provide(:title, 'Create a trip') %>
<h1>Creat a trip</h1>

<div class="trips">
 <%= form_for(@trip) do |f| %>
 <%= render 'fields', f: f %>

 <p>flight there</p>

 <%= form_for(@avisaler_there) do |builder| %>
  <%= render 'salers_fields', f: builder %>
 <% end %>

 <p>flight back</p>

 <%= form_for(@avisaler_back) do |builder| %>
  <%= render 'salers_fields', f: builder %>
 <% end %>

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

show.html.erb

<p>flight there</p>

<ol>
 <% for @avisaler_there in @trip.aviasalers %>
  <li><%= @avisaler_there.name %></li>
  <li><%= @avisaler_there.quantity %></li>
 <% end %>
</ol>

<p>flight back</p>

<ol>
  <% for @avisaler_back in @trip.aviasalers %>
  <li><%= @avisaler_back.name %></li>
  <li><%= @avisaler_back.quantity %></li>
  <% end %>
</ol>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Igor
  • 13
  • 6
  • How are you differentiating between `avisaler_there` and `avisaler_back` ? If you aren't then probably you should. – swapab Dec 22 '13 at 06:15
  • May be a boolean OR better would be to introduce another table `avisaler_types` that you can refer from your `Avisaler` model. Also see this http://stackoverflow.com/questions/4641565/rails-3-submit-a-form-with-multiple-records – swapab Dec 22 '13 at 06:23
  • @swapnilabnave, thanks for response. For example I can add ":back" attribute with boolean type to avisaler. How can I make two forms in new action where 1st form should create avisaler with attribute (back: false) and 2nd form should create aviasaler with (back: true)? – Igor Dec 22 '13 at 11:41

0 Answers0