RubyRedGrapefruit
RubyRedGrapefruit

Reputation: 12224

How can I put a link inside of a text string in HAML?

This should probably be easier than it is. I just want to put a link inside an HTML paragraph element.

%p{class: "answer"}="Please upload your data to this portal in text (tab-separated) format. Download our template #{raw(link_to 'here', '/templates/upload_template.xlsx')} for sample data and a description of each column."

Rails is encoding the tag information. I don't want tags to be encoded. I want them to be tags.

Upvotes: 1

Views: 1454

Answers (2)

matt
matt

Reputation: 79723

You can use interpolation directly in Haml, and doing this seems to fix the issue in this case.

So instead of doing this:

%p= "Text with #{something_interpolated} in it."

you can just do

%p Text with #{something_interpolated} in it.

i.e. you don’t need the = or the quotes, since you are just adding a single string. You shouldn’t need to use raw either.

(Also, note you can do %p.answer to set the class attribute, which may be cleaner if the value isn’t being set dynamically.)


Why this is being escaped here is a different matter. I would have expected the two cases (%p= "#{foo}" and %p #{foo}) to behave the same way. However, after a bit of research, this behaviour seems to match how Rails behaves with Erb.

Upvotes: 2

Juliano
Juliano

Reputation: 346

You can use more than one line inside any block, to solve your problem we will have something like this:

%p{class: "answer"}
  Please upload your data to this portal in text (tab-separated) format. Download our template 
  = link_to 'here', '/templates/upload_template.xlsx'
  for sample data and a description of each column."

Upvotes: 3

Related Questions