这是我使用的工作代码到底。感谢所有帮助过的人。遵循D方的方法,但使用“雇主”作为基础模型。我可以重构这个来删除Employer上的attr_accessors,而是在视图中使用字段标记。最后,我只是从传递给控制器的“params”数组中取出共享值。
预订控制器:
def new
@employer = current_employer
@booking = @employer.bookings.build
respond_with(@booking)
end
def create
errors = []
booking_create_params["new_booking_attributes"].each do |booking|
new_booking = current_employer.bookings.build(booking)
new_booking.job_type_id = booking_create_params["job_type_id"]
new_booking.vehicle_id = booking_create_params["vehicle_id"]
new_booking.location_id = booking_create_params["location_id"]
new_booking.pay = booking_create_params["pay"]
unless new_booking.save
errors << new_booking.errors.full_messages
end
end
if errors == []
flash[:notice] = "Bookings posted!"
redirect_to new_booking_path
else
flash[:notice] = "Error: #{errors.join(', ')}!"
redirect_to new_booking_path
end
end
在视图:
<div id="bookings">
<ol class="numbers">
<li>
<legend>Location, Pay, & Vehicle</legend>
<div class="form-group">
<div class="row">
<div class="col-sm-6">
<label>Type of job</label><br>
<%= f.select(:job_type_id, options_from_collection_for_select(JobType.all, :id, :name_with_delivery), {}, { id: 'job-type', class: 'form-control' }) %>
</div>
<div class="col-sm-6">
<label>Vehicle needed</label><br>
<%= f.select(:vehicle_id, options_from_collection_for_select(Vehicle.all, :id, :name), {}, { id: 'vehicle-type', class: 'form-control' }) %>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-6">
<label>Location</label>
<% if current_employer.locations.count > 1 %>
<%= f.select :location_id, options_from_collection_for_select(current_employer.locations.all, :id, :name_or_address_1), {}, { class: 'form-control' } %>
<% elsif current_employer.locations.count == 1 %>
<p><strong>Location: </strong><%= current_employer.locations.first.name_or_address_1 %></p>
<%= f.hidden_field :location_id, current_employer.locations.first.id %>
<% end %>
<%= link_to "or add new location", new_employer_location_path(current_employer, Location.new) %>
</div>
<div class="col-sm-6">
<%= f.label :pay %><br>
<%= f.text_field :pay, class: 'form-control' %>
</div>
</div>
</div>
</li>
<legend>Shifts</legend>
<%= render 'booking', booking: Booking.new %>
</ol>
</div>
其中部分是:
<li class="close-list-item">
<!-- Recommended: post at least 1 week in advance
& shift length at least 4 hours.
-->
<% new_or_existing = booking.new_record? ? 'new' : 'existing' %>
<% prefix = "employer[#{new_or_existing}_booking_attributes][]" %>
<%= fields_for prefix, booking do |booking_form| -%>
<div class="shifts">
<div class="form-group shift">
<div class="row">
<div class="col-sm-4">
<label for="">Date</label>
<i class="glyphicon glyphicon-time"></i>
<%= booking_form.text_field :start, class: 'form-control booking-date', placeholder: 'Date', data: { provide: "datepicker", date_clear_btn: "true", date_autoclose: "true", date_start_date: '+1d', date_format: "yyyy-mm-dd" } %>
</div>
<div class="col-sm-4">
<label>Time Start</label><br>
<%= booking_form.select(:start_time, options_for_select(
booking_times_array
), { include_blank: true }, { class: 'form-control booking-time booking-time-start' }) %>
</div>
<div class="col-sm-4">
<label>
Time End
</label>
<%= link_to "javascript:;", class: 'pull-right remove-shift' do %>
<i class="glyphicon glyphicon-remove"></i>
<% end %>
<script type="text/javascript">
$(".remove-shift").click(function(){
$(this).parents("li").remove();
});
</script>
<%= booking_form.select(:end_time, options_for_select(
booking_times_array
), { include_blank: true }, { class: 'form-control booking-time booking-time-end' }) %>
</div>
</div>
</div>
</div>
</li>
<% end -%>
你描述的前端,对背什么结束?应该填写什么数据? –
更新后的描述 –
我对你的术语有些困惑。当你说“模型”时,你的意思是“模型类的记录/实例”吗? –