0

I'm trying to do this:

headline = 'Created a new' && link_to('Set', set_path(set))

Also tried:

headline = 'Created a new' + link_to('Set', set_path(set))

but it does't output like:

Created a new <a href="xxxx">Set</a>

Ideas? thxs

0

2 Answers 2

1

Rails has a nifty XSS prevention feature that might be getting in your way here. Before outputting strings, it will encode any HTML found in them by default, unless they were explicitly marked as safe strings ahead of time. link_to runs html_safe! on its output, which is why it isn't usually encoded, but adding it to an unsafe string like 'Created a new' removes that HTML-safe attribute.

Try calling:

headline.html_safe!

immediately after setting it.

Of course, since the question doesn't specify what output you get instead of what you want, I can't be totally sure that this is the issue. But it's the best I can do with what I've got :)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, how should I create the headline var? I show'd a few ideas up top, not sure which if any are right.
Appending the string with + is correct. && is used for boolean expressions, e.g. obj.something_is_true? && obj.somthing_else_is_true?, whereas + will concatenate two strings :)
0

I would just put this straight into your view, or in a helper. You can also simplify your link_to somewhat:

<%= 'Created a new' + link_to('Set', set) %>
# or 
<%= "Created a new #{link_to('Set', set)}" %>
# or 
Created a new <%= link_to('Set', set) %>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.