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>