Here's the string I want:
<a href="/pugs/1-baxter">Baxter</a> and <a href="/pugs/2-sofia">Sofia</a>
Here's the code I'm using to output that:
<%= @pugs.collect {|p| link_to(p.name, pug_path(p))}.to_sentence %>
Unfortunately the output is getting encoded:
<a href="/pugs/1-baxter">Baxter</a> and <a href="/pugs/2-sofia">Sofia</a>
I've tried using html_safe
and raw
, but they don't seem to have any affect.
As of Rails 5, there's a to_sentence
view helper (different from Array#to_sentence
) that does this.
From the docs:
to_sentence(array, options = {})
Converts the array to a comma-separated sentence where the last element is joined by the connector word. This is the html_safe-aware version of ActiveSupport's Array#to_sentence.
Use it like: <% to_sentence(@pugs.collect {|p| link_to(p.name, pug_path(p))}) %>
<%= @pugs.collect {|p| link_to(p.name, pug_path(p))}.to_sentence.html_safe %>
You could wrap it in a span and use a helper:
def content_link_to(name,path=nil,options=nil)
content_tag :span do
link_to name, path, options
end
end
And use it as follows:
<%= @pugs.collect {|p| content_link_to(p.name, pug_path(p))}.to_sentence %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With