我有部分表单,允许用户输入将包含在出站电子邮件中的主题和消息。我想允许用户从与电子邮件所属的发票关联的联系人中选择电子邮件的收件人。通过嵌套表单选择的电子邮件收件人将存储在单独的表中。
class EmailRecipient < ActiveRecord::Base
attr_accessible :contact_id, :email_id
belongs_to :email
end
class Email < ActiveRecord::Base
attr_accessible :subject, :message, :invoice_id, :email_recipients_attributes
belongs_to :invoice
has_many :email_recipients
accepts_nested_attributes_for :email_recipients
end
<%= simple_form_for [:invoice, @email], html: {class: "form-horizontal"} do |f| %>
<%= f.error_notification %>
<% @invoice.contacts do |c|%>
<%= f.fields_for :email_recipients do |builder| %>
<%= builder.input :contact_id, :as => :check_boxes %>
<%= c.name %><br/>
<% end %>
<% end %>
<%= f.input :subject, :as => "string" %>
<%= f.input :message, :input_html => { :class => 'span7', :rows => 10 } %>
<div class="form-actions">
<%= f.button :submit, "Send Invoice", :class => 'btn-warning' %>
<%= link_to 'Cancel', invoice_path(@invoice), :class => 'btn' %>
</div>
<% end %>