Home > Mobile >  Using Loop Variable to Set the id Attribute
Using Loop Variable to Set the id Attribute

Time:10-11

I have the following code:

<% @electives.each do |elective| %>
  <div>
  </div>
<% end %>  

I would like to set the id of the div to elective.name, but I don't know how to do it, or whether this works:

<% @electives.each do |elective| %>
  <div id="elective.name">
  </div>
<% end %>  

Is it possible to do this in Rails?

Thanks.

CodePudding user response:

You can use <%= ruby variable %> block within erb file to add ruby values within your pages

<div id="<%= elective.name %>">

CodePudding user response:

As an alternative solution, you can use the rails tag helper

<% @electives.each do |elective| %>
  <%= tag.div id: elective.name do %>
    ... 
  <% end %> 
<% end %> 
  • Related