John Merlino
2012-Mar-21 19:15 UTC
update a list of model instances and their associations
I have been unable to figure out how to update a list of model
instances and their associations and I have looked in the
documentation, which doesnt give an example of what I am trying to do:
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for
I would assume this is a common task though.
I have an established association:
class AlertRule < ActiveRecord::Base
has_many :notification_emails, :dependent => :destroy
accepts_nested_attributes_for :notification_emails
end
class NotificationEmail < ActiveRecord::Base
belongs_to :alert_rule
end
Controller:
def alerts_config
//more code
@alert_rules = alert_rules.flatten
@alert_rules.each { |a| a.notification_emails.build }
render :partial => ''home/alerts_config'', :layout
=> false
View:
= form_tag ''/home/save_alerts_config/'' + @unit.id.to_s,
:remote =>
true, :class => ''ajaxForm'' do
%table.scrollTable{:cellspacing => "0", :width =>
"740px", :style => "border-collapse: collapse; border-spacing:
0;"}
%thead.fixedHeader
%tr
%th Alerts
%th Enable
%th Primary Email
%th Notification Emails
%th
%tbody.scrollContent
- for rule in @alert_rules
= fields_for :alert_rules, rule do |f|
%tr
%td= rule.alert_code.name
%td= check_box_tag "enabled_ids[]", rule.id
%td= f.text_field :email, :value => rule.email, :index
=> rule.id
%td.fields
= f.fields_for :notification_emails do |
notification_builder|
= notification_builder.text_field :email
notification_builder.hidden_field :_destroy
= link_to_function ''Remove Notification'',
''remove_notifications(this)''
.save_panel
= submit_tag "Save", :class => ''submit
myButton''
I look at the name attribute of one of the notification email fields,
and it looks like this:
<input id="alert_rules_notification_emails_attributes_0_email"
name="alert_rules[notification_emails_attributes][0][email]"
size="30"
type="text">
However, it should look like this:
<input id="alert_rules_notification_emails_attributes_0_email"
name="alert_rules[51][notification_emails_attributes][0][email]"
size="30" type="text">
So what is missing is the association between this alert rule and its
notification emails. I am not sure how to make it look like the above.
But because it doesnt contain the id in brackets of the alert rule,
when I post, i get this back:
Started POST "/home/save_alerts_config/6243" for 127.0.0.1 at
2012-03-21 15:12:22 -0400
Processing by HomeController#save_alerts_config as
Parameters: {"utf8"=>"✓",
"authenticity_token"=>"7y9qukke1n35xTXaWPPL9deTmgQq096wqh+H1APeSAk=",
"enabled_ids"=>["51", "52"],
"alert_rules"=>{"51"=>{"email"=>"hythy"},
"notification_emails_attributes"=>{"0"=>{"email"=>"",
"_destroy"=>"false"}},
"52"=>{"email"=>"yutu"},
"53"=>{"email"=>"ytuytu"},
"54"=>{"email"=>""},
"55"=>{"email"=>""},
"56"=>{"email"=>""},
"57"=>{"email"=>""},
"58"=>{"email"=>""},
"59"=>{"email"=>""},
"60"=>{"email"=>""},
"61"=>{"email"=>""},
"62"=>{"email"=>""},
"63"=>{"email"=>""},
"64"=>{"email"=>""},
"65"=>{"email"=>""},
"66"=>{"email"=>""},
"67"=>{"email"=>""},
"68"=>{"email"=>""},
"69"=>{"email"=>""},
"70"=>{"email"=>""},
"71"=>{"email"=>""},
"72"=>{"email"=>""},
"73"=>{"email"=>""},
"74"=>{"email"=>""},
"75"=>{"email"=>""},
"76"=>{"email"=>""},
"77"=>{"email"=>""},
"78"=>{"email"=>""},
"79"=>{"email"=>""},
"80"=>{"email"=>""},
"81"=>{"email"=>""},
"82"=>{"email"=>""},
"83"=>{"email"=>""},
"84"=>{"email"=>""},
"85"=>{"email"=>""},
"86"=>{"email"=>""},
"87"=>{"email"=>""},
"88"=>{"email"=>""},
"89"=>{"email"=>""},
"90"=>{"email"=>""},
"91"=>{"email"=>""}},
"id"=>"6243"}
It has this:
"52"=>{"email"=>"yutu"}
thats supposed to be another alert rule, not a notification email of
alert rule 51, and this occurs because the name attribute is not
including the id of the alert rule of the notification email.
thanks at all for any possible workaround for this
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.