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

--
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@googlegroups.com.
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.

Search Discussions

  • Stephen bartholomew at Mar 18, 2012 at 3:45 pm
    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

    On Friday, March 16, 2012 9:02:35 PM UTC, John Merlino wrote:

    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
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/UbU58GczYuYJ.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedMar 16, '12 at 9:02p
activeMar 18, '12 at 3:45p
posts2
users2
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2021 Grokbase