Search code examples
jqueryruby-on-railscssajaxprototypejs

How do I make my div IDs variable in rails?


I feel like there should be a simple way to do this, but I just don't know what it is. I have a list of data to display, and I want to include an AJAX "Read More" function to expand information at the bottom of each segment. To do this, I need unique div IDs within each segment. I have this code:

<% for choice in @student_choices %>
    <div id= "student_description", style="display:none;">
        <%= choice[:description]%>
    </div>
    <%= link_to_function "Read More", "Element.show("student_description")"%>
<% end %>

But since there are multiple div IDs, clicking "Read More" only displays the first one. How do I insert a variable div ID? I know how to do this in PHP, but this has me stumped. Even if we get the div ID itself to be variable, how do we get Element.show to accept a variable?


Solution

  • You can do something like:

    <% div_id = "student_description_#{choice.id}" -%>
    
    <div id="<%= div_id -%>", style="display:none;">
        <%= choice.description %>
    </div>
    
    <%= link_to_function "Read More", "Element.show('##{div_id}')" %>