开发者

Rails Creating a Variable with text and a link_to

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜