Baxter and Sofia Here i...">

To_sentence and html_safe together?

Here is the line I want:

<a href="/pugs/1-baxter">Baxter</a> and <a href="/pugs/2-sofia">Sofia</a> 

Here is the code I use for output:

 <%= @pugs.collect {|p| link_to(p.name, pug_path(p))}.to_sentence %> 

Unfortunately, the result is encoded:

  &lt;a href=&quot;/pugs/1-baxter&quot;&gt;Baxter&lt;/a&gt; and &lt;a href=&quot;/pugs/2-sofia&quot;&gt;Sofia&lt;/a&gt; 

I tried using html_safe and raw , but they don't seem to be affected.

+4
source share
3 answers
 <%= @pugs.collect {|p| link_to(p.name, pug_path(p))}.to_sentence.html_safe %> 
+2
source

Like Rails 5, the to_sentence helper element (other than Array#to_sentence ) is used there.

From documents :

to_sentence (array, options = {})
Converts an array to a sentence, separated by commas, where the last element is connected to the connecting word. This is the version of ActiveSupport that supports the html_safe Array # to_sentence.

Use it as: <% to_sentence(@pugs.collect {|p| link_to(p.name, pug_path(p))}) %>

+2
source

You can wrap it in between 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 %> 
+1
source

Source: https://habr.com/ru/post/1401303/


All Articles