[英]Rails Link to a Class or iterate an Id
我的應用程序中有一部分顯示消息。 我正在嘗試使用一個燈箱來顯示單擊時每條消息的一部分。 我的問題是我正在使用ID鏈接到隱藏的div。 由於我使用的是ID,因此鏈接只會顯示第一條消息,而不是隨后的每條消息。 如何更改此設置,以便每條重復的消息都將顯示其自己的消息,而不是第一條消息?
我在下面包含了我的代碼。 我正在使用fancybox燈箱。
謝謝您的幫助。
<tbody>
<% @messages.each do |m| %>
<tr>
<td><%= m.from.name %></td>
<td><%= truncate(m.topic, length: 30) %></td>
<td><%= link_to truncate(m.body, length: 35), "#user_message", class: 'fancybox' %></td>
<td>
<%= link_to 'Mark Read', '#', confirm: 'Are you sure?', :class => 'btn btn-mini' %>
<%= link_to 'Reply', '#', confirm: 'Are you sure?', :class => 'btn btn-mini btn-inverse' %>
<%= link_to 'Delete', '#', confirm: 'Are you sure?', :class => 'btn btn-mini btn-danger' %>
</td>
</tr>
<div style="display:none" id="user_message">
<%= m.body %>
</div>
<% end %>
</tbody>
一個簡單的解決方案是通過附加一些唯一的東西(例如說消息的數字ID)來強制ID唯一。
<% @messages.each do |m| %>
<tr>
...
<td><%= link_to truncate(m.body, length: 35), "#user_message-#{m.id}", class: 'fancybox' %></td>
...
</tr>
<div style="display: none" id="user_message-<%= m.id %>">
...
在不查看JQuery代碼的情況下很難告訴您確切的答案,但是通常情況下,執行此類操作的方式是附加動態值以區分div
例如:
<tbody>
<% @messages.each do |m| %>
<tr>
<td><%= m.from.name %></td>
<td><%= truncate(m.topic, length: 30) %></td>
<td><%= link_to truncate(m.body, length: 35), "#user_message{m.id}", class: 'fancybox' %></td>
<td>
<%= link_to 'Mark Read', '#', confirm: 'Are you sure?', :class => 'btn btn-mini' %>
<%= link_to 'Reply', '#', confirm: 'Are you sure?', :class => 'btn btn-mini btn-inverse' %>
<%= link_to 'Delete', '#', confirm: 'Are you sure?', :class => 'btn btn-mini btn-danger' %>
</td>
</tr>
<div style="display:none" id="user_message{m.id}">
<%= m.body %>
</div>
<% end %>
</tbody>
注意{m.id}
部分
高溫超導
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.