我实际上是通过Rails指南中的设置使用示例has_many
:http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association
我有一个名为options
的模型,另一个名为registrations
。当您注册某个活动时,您可以选择多个options
,并且每个选项都有自己的时间和与会者限制,这些是注册条目所特有的
我的联接表格为registrations_options
,其中option_id
和registration_id
填充正常。但是连接表还包含名为time
和attendee_limit
的列。对于我的生活,我无法弄清楚如何从表格中填充它们。
我已经尝试了很多东西,但这只是本节表单代码的最后一个试用状态:
#models/registration.rb
class Registration < ApplicationRecord
belongs_to :event
has_many :registrations_options
has_many :options, through: :registrations_options
...
#models/option.rb
class Option < ApplicationRecord
belongs_to :church
has_many :registrations_options
has_many :registrations, through: :registrations_options
...
#registrations_controller.rb
...
def new
@registration = Registration.new
@registration_form = RegistrationForm.new(@registration, registrations_path)
end
...
#admin/registrations/partials/_form.html.erb
<%= form_for @registration_form do |f| %>
<%= render partial: '/admin/registrations/partials/options', locals: {options: @registration_form.options, selected_church: @registration_form.selected_church, registration: @registration_form.registration} %>
...
#admin/registrations/partials/_options.html.erb
<%= collection_check_boxes :registration, :option_ids, options, :id, :ministry_area do |b| %>
<%= b.check_box + " " + b.label %><br/>
<div class="field" id="additional-info-<%= b.object.id %>">
<fieldset class="fieldset">
<legend>Additional Information</legend>
<div class="additional-info-container">
<div class="field">
<%= label_tag "What time do you want to observe the environment(s)?" %>
<div class="radio">
<% b.object.available_service_times.split(', ').each do |time| %>
<%= radio_button_tag 'registrations_options[service_time][' + b.object.id.to_s + ']', time %> <%= time %><br> #this is one of the lines in question!
<% end %>
</div>
<div class="field">
<%= label_tag " How many people will be observing this environment?" %>
<%= select_tag 'registrations_options[attendees]', options_for_select((1 .. b.object.attendee_limit).to_a) %> #this is the other line in question!
</div>
</div>
</div>
</fieldset>
</div>
<% end %>