Ich habe das RailsCast-Video zum Erstellen verschachtelter Formulare verfolgt: http://railscasts.com/episodes/196-nested-model-form-part-1?autoplay=trueRuby on Rails: Warum funktioniert mein verschachteltes Formular nicht?
Aber aus irgendeinem Grund spart es nicht.
Beim Erstellen einer E-Mail versuche ich Datensätze in der Empfängertabelle zu erstellen, so dass aufgezeichnet werden kann, an welche Gruppen und Kontakte die E-Mail gesendet wurde. Das funktioniert, aber ich versuche auch, Daten in einer Spalte namens "Nachricht" in dieser Tabelle zu speichern, aber aus irgendeinem Grund wird der neue Datensatz in der Empfängertabelle erstellt, aber die Nachricht wird nicht in der Tabelle gespeichert.
Meine Modelle sind:
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
Meine emails_controller
neue und erstellen Methoden sind:
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
Und mein _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 %>
ist Kann mir jemand bitte ausrechnen helfen, warum die message
Feld wird nicht in t gespeichert er Empfängertabelle, während eine neue Zeile in der Tabelle erstellt wird?
Wie sieht Ihre Erstellungsaktion aus? – Iceman
@Iceman Ich habe gerade hinzugefügt, dass –