开发者

Rails Creating a Variable with text and a link_to

开发者 https://www.devze.com 2023-02-01 10:58 出处:网络
I\'m trying to do this: headline = \'Created a new\' && link_to(\'Set\', set_path(set)) Also tried:

I'm trying to do this:

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

Also tried:

headline = 'Created 开发者_JAVA技巧a new' + link_to('Set', set_path(set))

but it does't output like:

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

Ideas? thxs


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 :)


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) %>
0

精彩评论

暂无评论...
验证码 换一张
取 消