简体   繁体   中英

Ruby on rails - each do class?

<% consents_checkboxes.each do |checkbox| %>
  <%= checkbox.html_safe %>
<% end %>

Hello there,

can i give them a class while looping through them? I can't get it to work and tried several different ways.

This is something I would like to achieve

<% consents_checkboxes.each do |checkbox| %>
  <%= checkbox.html_safe, class: 'checkbox' %>
<% end %>

thank you

You can only do it with an element. What you want to do is:

<% consents_checkboxes.each do |checkbox| %>
   <p class="checkbox"><%= checkbox.html_safe %></p>
<% end %>

Of course, you can use another element (span, div etc.).

What's on consents_checkboxes? You should provide more context when you ask for something...

It looks like you have strings with the html code, right? you will have to parse the string with something like nokogiri and add a class

<%= Nokogiri.parse(checkbox).add_class('checkbox') -%>

Or you could modify the process that generates that consents_checkboxes to include the class you need. Maybe there's better options, but with only that information it's really hard to tell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM