RailsCastビデオをネストされたフォームに作成しました。http://railscasts.com/episodes/196-nested-model-form-part-1?autoplay=trueRuby on Rails:なぜ私のネストされたフォームは機能しませんか?
しかし何らかの理由で保存されません。
電子メールを作成するときに、受信者テーブルにレコードを作成しようとしているため、電子メールが送信されたグループと連絡先が記録されます。これは動作しますが、このテーブルの「メッセージ」という列にデータを保存しようとしていますが、何らかの理由で受信者テーブルの新しいレコードが作成されますが、メッセージはテーブルに保存されません。
私のモデルは以下のとおりです。
class Email < ActiveRecord::Base
belongs_to :account
has_many :recipients
has_many :contacts, through: :recipients, :dependent => :destroy
has_many :groups, through: :recipients, :dependent => :destroy
accepts_nested_attributes_for :recipients
end
class Recipient < ActiveRecord::Base
belongs_to :email
belongs_to :contact
belongs_to :group
end
私の新しいemails_controller
とメソッドを作成するには、次のとおりです。
def new
@email = Email.new
@email.recipients.build
@useraccounts = Useraccount.where(user_id: session[:user_id])
end
def create
@email = Email.new(email_params)
if @email.save
redirect_to @email
else
render 'new'
end
end
private
def email_params
params.require(:email).permit(:subject, :account_id, { contact_ids: [] }, { group_ids: [] }, recipient_attributes: [:message])
end
そして、私の_form.html.erb
は、誰かが私がなぜうまく助けてください
<%= form_for @email do |f| %>
<% if @email.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(@email.errors.count, "error") %> prohibited this email from being saved:
</h2>
<ul>
<% @email.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :account_id, "Send from account" %><br>
<% @useraccounts.each do |useraccount| %>
<%= f.radio_button :account_id, useraccount.account_id, :checked => false %>
<%= f.label :account_id, useraccount.account.email, :value => "true" %><br>
<% end %>
</p>
<p>
<%= f.label :subject %><br>
<%= f.text_field :subject %>
</p>
<p>
<%= f.label :contacts, "Send to Contacts:" %><br>
<%= f.collection_check_boxes :contact_ids, Contact.where(user_id: session[:user_id]), :id, :firstname ,{ prompt: "firstname" } %>
</p>
<p>
<%= f.label :groups, "Send to Groups:" %><br>
<%= f.collection_check_boxes :group_ids, Group.where(user_id: session[:user_id]), :id, :name ,{ prompt: "name" } %>
</p>
<%= f.fields_for :recipients do |t| %>
<%= t.label :message %>
<%= t.text_field :message %>
<% end %>
<p>
<%= f.submit %>
</p>
<% end %>
ですmessage
フィールドがtに保存されていませんテーブルに新しい行が作成されている間に受信者のテーブルが表示されますか?
作成アクションはどのように見えますか? – Iceman
@Iceman私はちょうどそれを追加しました –