Calling 'sanitize' on collection_radio_buttons text_method

20 views
Skip to first unread message

Serguei Cambour

unread,
Oct 26, 2016, 6:45:48 AM10/26/16
to Ruby on Rails: Talk
How is it possible to call sanitize on the below collection_radio_buttons method:

= f.collection_radio_buttons :answer_ids, @question.answers, :id



I tried as follows and it failed:

= f.collection_radio_buttons :answer_ids, @question.answers, :id, sanitize(:text, tags: %w(sub sup))

Any idea? Thank you.

Serguei Cambour

unread,
Oct 27, 2016, 6:54:07 AM10/27/16
to Ruby on Rails: Talk
I meant how to change or customise a radio button label text so that to be able to display Math and chemistry formulas using
<sub>

and
<sup>

tags.

Serguei Cambour

unread,
Oct 27, 2016, 8:26:40 AM10/27/16
to Ruby on Rails: Talk
I figured out how to achieve that:

<%= f.collection_radio_buttons :name, [[true, 'H<sub>2</sub>O'] ,[false, 'SO<sub>2</sub>']], :first, :last do |b| %>
       
<%= b.label {b.radio_button + sanitize(b.text, tags: %w(sub sup))}%>
 
<%end%>


On Wednesday, 26 October 2016 12:45:48 UTC+2, Serguei Cambour wrote:

Rob Biedenharn

unread,
Oct 27, 2016, 5:54:38 PM10/27/16
to rubyonra...@googlegroups.com
While I wouldn't normally suggest this for user-supplied data, you could change the Answer#text to return .sanitize'd strings:

class Answer

  include ActionView::Helpers::SanitizeHelper 
  def text
    sanitize(read_attribute(:text), tags: %w[sub sup])
  end

end

think that this will work, but I haven't tried it myself.

-Rob

Serguei Cambour

unread,
Oct 28, 2016, 3:14:56 AM10/28/16
to Ruby on Rails: Talk
Yes, Rod, thank you for the idea. I knew that, it would work. I don't want to mess the View stuff with the the Model ones. The input will be done by admins only (back office), so this kind of GUI will have no danger and will make it possibe for them to encode formulas and a final user will see the desired result as needed.

Regards


On Wednesday, 26 October 2016 12:45:48 UTC+2, Serguei Cambour wrote:
Reply all
Reply to author
Forward
0 new messages