Brian Hicks
Brian Hicks

Reputation: 6403

Unescaping inline links in haml

So I'm trying to get a remove link next to the caption in an emails form. However, my rails voodoo is clearly very weak. It escapes the HTML and I can't figure out how to get it to not. Any hints?

= f.input :email, :label => "Email " + link_to_remove_fields("[x]", f)

[edit] oh and yes, this does the same thing:

= f.input :email, :label => "Email #{link_to_remove_fields("[x]", f)}"

and here's my helper method...

def link_to_add_fields(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(association.to_s.singularize + "_fields", :f => builder)
    end
    link_to_function(name, "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")")
  end

Upvotes: 0

Views: 1678

Answers (3)

Kevin Ansfield
Kevin Ansfield

Reputation: 2343

I had the same issue as this, I fixed it by using the following in my helper:

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

One thing to note, when I tried this in a Rails 3.1 app I had to restart the server before changes in the helper were picked up. This may or may not be true for earlier versions but worth keeping in mind.

Upvotes: 1

dombesz
dombesz

Reputation: 7899

Try this:

require 'cgi'

unescaped = CGI.unescapeHTML(string)

Upvotes: 0

quest
quest

Reputation: 776

This isn't too hard to fix. Rails will escape everything by default. In order to prevent that you can use the method html_safe on any string. This would fix your issue.

= f.input :email, :label => "Email " + link_to_remove_fields("[x]", f).html_safe

or

= f.input :email, :label => "Email #{link_to_remove_fields("[x]", f).html_safe}"

Remember not to put it at the end of the entire tokenized string, or it won't work. For instance, this won't work.

= f.input :email, :label => "Email #{link_to_remove_fields("[x]", f)}".html_safe

Hope this helps you out. =)

Upvotes: 4

Related Questions