Nested attribute relationship never renders in form

Hey all,

I tried following this railscasts:

http://railscasts.com/episodes/196-nested-model-form-part-1

But there is one significant difference from what that does and what I
am trying to do. That builds a relationship to one object. In my form,
i have a list of objects and want to build relationship for each item
in list.

class Rule < ActiveRecord::Base
has_many :notification_emails, :dependent => :destroy
accepts_nested_attributes_for :notification_emails, :reject_if =>
lambda { |notification| notification[:email].blank? }
end

class NotificationEmail < ActiveRecord::Base
belongs_to :rule
end

#home_controller

def rules_config

@rules = rules.flatten
@rules.each { |a| a.notification_emails.build }

  render :partial => 'home/rules_config', :layout => false

end

#home/rules_config.html.haml

#config
.form_content
= form_for “rule[]”, ‘/home/save_rules/’ + @unit.id.to_s, :remote
=> true, :class => ‘ajaxCall’ do |f|
%table.scrollTable{:cellspacing => “0”, :width =>
“740px”, :style => “border-collapse: collapse; border-spacing: 0;”}
%thead.fixedHeader
%tr
%th Rules
%th Enable
%th Email
%th Notification Emails
%tbody.scrollContent
- for rule in @rules
%tr
%td= rule.rule_code.name
%td= check_box_tag “enabled_ids[]”, rule.id
%td= f.text_field :email, :value => rule.email, :index
=> rule.id
%td
- f.fields_for :notification_emails do |notification|
= notification.text_area :email, :rows => 1

Basically I have a list of rules that a user can edit and then update
in this form. But each rule can have many notification emails and
since I built an association in controller, I expect this cell to
contain a text area field that links the relatonship. But the text
area field never displays.

thanks for response

Depending on which Rails version you’re using, you might need to change:

  • f.fields_for :notification_emails do |notification|

to:

= f.fields_for :notification_emails do |notification|

(notice the = change)

Steve

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs