我有一个非常具体的问题,困扰我一天。尝试了许多不起作用的解决方案,所以我来堆栈溢出。
问题描述:
所以我有一个显示objecs(Rabbit)行的页面,对于每一行,我试图创建一个调用javascript函数的按钮,该函数显示并隐藏包含另一个与该兔子对象相关的血清表的分区。 一只兔子有很多血清,因此有桌子。
问题: 每当我点击按钮显示表格时,它总是显示第一行的血清,即第一个兔子对象。
以下是我的代码。 请查看并指出任何明显的错误或任何想到的解决方案 感谢
控制器方法:
def lampire_rabbit_list
@rabbits = PolyAnimal.lampire_rabbits
end
模型方法:
<%= javascript_include_tag :defaults, 'sortable' %>
<%= stylesheet_link_tag "peptide" %>
<h1> Lampire Rabbits </h1>
<style>
div#serums
{
display:none;
}
</style>
<script>
function showHideTable()
{
var status = document.getElementById("serums")
status.style.display = (status.style.display == "table") ? "none" : "table";
}
</script>
<table class="sortable" cellpading="5" cellspacing="2" width="100" >
<tr>
<th> Rabbit Number </th>
<th> Location </th>
<th> Group Name </th>
<th> Transition </th>
<th> Notes </th>
<th> Current Injected Lots</th>
<th> Show Serums </th>
</tr>
<% for rabbit in @rabbits %>
<% vendor = rabbit.vendor.name rescue 'NA' %>
<% serums = rabbit.serums %>
<tr valign = "top" class= "<%= cycle('color_one', 'color_two') %>">
<td><%= rabbit.animal_number %></td>
<td><%= rabbit.location %></td>
<td><%= vendor %></td>
<td><%= rabbit.transition%></td>
<td><%= rabbit.notes%></td>
<td><%= rabbit.current_number_injected_lots %></td>
<td id = "linker"><button onclick = "showHideTable();">click to show</button>
<div id = "serums">
<table>
<%for serum in serums%>
<tr><td align = "center"> <%= link_to "#{serum.bleed_date.strftime("%d-%m-%Y")}", :controller => 'serum', :action => 'report', :id => serum.id rescue ''%></td></tr>
<%end%>
</table>
</div>
</td>
</tr>
<%end%>
</table>
答案 0 :(得分:0)
我只是想通了。 我不得不将每只兔子的兔子ID作为变量传递给javascript函数。 DOH !!!!