I have a form helper that looks like this:

#_alert.html.haml
= add_field_link "Add Email", f, :notification_emails

#application_helper.rb
def add_field_link(name, f, association)
new_object =
f.object.class.reflect_on_association(association).klass.new
fields = f.fields_for(association, new_object, :child_index =>
"new_#{association}") do |builder|
render("home/" + association.to_s.singularize + "_fields", :f =>
builder)
end
link_to_function(name,
h("add_fields(this,'#{association}','#{escape_javascript(fields)}')"))
end

:javascript
function add_fields(link, association, content) {
var new_id = new Date().getTime();
var regexp = new RegExp('new_' + association, 'g');
$(link).prev('ul').append('<li>' + content.replace(regexp, new_id)
+ '</li>');
}

This line here I had to use ' rather than "/ because otherwise
firebug will raise error "unexpected token &":

link_to_function(name,
h("add_fields(this,'#{association}','#{escape_javascript(fields)}')"))


but when I click on link its supposed to add a new field with model
attributes but doesnt. I look in firebug console and see this:

<a href="#" onclick="add_fields(this,'notification_emails','&amp;lt;li
class=\'fields\' style=\'list-style-type: none;\'&amp;gt;\n
&amp;lt;input id=
\&amp;quot;alert_rule_notification_emails_attributes_new_notification_emails_email
\&amp;quot; name=\&amp;quot;alert_rule[notification_emails_attributes]
[new_notification_emails][email]\&amp;quot; size=\&amp;quot;
30\&amp;quot; type=\&amp;quot;text\&amp;quot; /&amp;gt;\n
&amp;lt;input id=
\&amp;quot;alert_rule_notification_emails_attributes_new_notification_emails__destroy
\&amp;quot; name=\&amp;quot;alert_rule[notification_emails_attributes]
[new_notification_emails][_destroy]\&amp;quot; type=\&amp;quot;hidden
\&amp;quot; value=\&amp;quot;false\&amp;quot; /&amp;gt;\n
&amp;lt;input id=
\&amp;quot;alert_rule_notification_emails_attributes_new_notification_emails__destroy
\&amp;quot; name=\&amp;quot;alert_rule[notification_emails_attributes]
[new_notification_emails][_destroy]\&amp;quot; type=\&amp;quot;hidden
\&amp;quot; value=\&amp;quot;false\&amp;quot; /&amp;gt;&amp;lt;a href=
\&amp;quot;#\&amp;quot; onclick=\&amp;quot;remove_fields(this); return
false;\&amp;quot;&amp;gt;remove&amp;lt;\/a&amp;gt;\n&amp;lt;\/
li&amp;gt;\n'); return false;">Add Email</a>


Any idea why it looks like this and why the expected behavior doesnt
occur?

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

  • John Merlino at Mar 27, 2012 at 9:20 pm
    This was a simple fix. You dont escape html in rails 3.
    On Mar 26, 5:38 pm, John Merlino wrote:
    I have a form helper that looks like this:

    #_alert.html.haml
    = add_field_link "Add Email", f, :notification_emails

    #application_helper.rb
    def add_field_link(name, f, association)
    new_object =
    f.object.class.reflect_on_association(association).klass.new
    fields = f.fields_for(association, new_object, :child_index =>
    "new_#{association}") do |builder|
    render("home/" + association.to_s.singularize + "_fields", :f =>
    builder)
    end
    link_to_function(name,
    h("add_fields(this,'#{association}','#{escape_javascript(fields)}')"))
    end

    :javascript
    function add_fields(link, association, content) {
    var new_id = new Date().getTime();
    var regexp = new RegExp('new_' + association, 'g');
    $(link).prev('ul').append('<li>' + content.replace(regexp, new_id)
    + '</li>');
    }

    This line here I had to use ' rather than "/  because otherwise
    firebug will raise error "unexpected token &":

    link_to_function(name,
    h("add_fields(this,'#{association}','#{escape_javascript(fields)}')"))

    but when I click on link its supposed to add a new field with model
    attributes but doesnt. I look in firebug console and see this:

    <a href="#" onclick="add_fields(this,'notification_emails','&amp;lt;li
    class=\'fields\' style=\'list-style-type: none;\'&amp;gt;\n
    &amp;lt;input id=
    \&amp;quot;alert_rule_notification_emails_attributes_new_notification_email s_email
    \&amp;quot; name=\&amp;quot;alert_rule[notification_emails_attributes]
    [new_notification_emails][email]\&amp;quot; size=\&amp;quot;
    30\&amp;quot; type=\&amp;quot;text\&amp;quot; /&amp;gt;\n
    &amp;lt;input id=
    \&amp;quot;alert_rule_notification_emails_attributes_new_notification_email s__destroy
    \&amp;quot; name=\&amp;quot;alert_rule[notification_emails_attributes]
    [new_notification_emails][_destroy]\&amp;quot; type=\&amp;quot;hidden
    \&amp;quot; value=\&amp;quot;false\&amp;quot; /&amp;gt;\n
    &amp;lt;input id=
    \&amp;quot;alert_rule_notification_emails_attributes_new_notification_email s__destroy
    \&amp;quot; name=\&amp;quot;alert_rule[notification_emails_attributes]
    [new_notification_emails][_destroy]\&amp;quot; type=\&amp;quot;hidden
    \&amp;quot; value=\&amp;quot;false\&amp;quot; /&amp;gt;&amp;lt;a href=
    \&amp;quot;#\&amp;quot; onclick=\&amp;quot;remove_fields(this); return
    false;\&amp;quot;&amp;gt;remove&amp;lt;\/a&amp;gt;\n&amp;lt;\/
    li&amp;gt;\n'); return false;">Add Email</a>

    Any idea why it looks like this and why the expected behavior doesnt
    occur?

    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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedMar 26, '12 at 9:38p
activeMar 27, '12 at 9:20p
posts2
users1
websiterubyonrails.org
irc#RubyOnRails

1 user in discussion

John Merlino: 2 posts

People

Translate

site design / logo © 2021 Grokbase