在 Rails 中将表格行变成链接 [英] Making a table row into a link in Rails
问题描述
我正在尝试在指向编辑页面的表格链接中创建一行.我知道正在创建链接,因为我可以将它们打印出来.我很接近,但我错过了一些重要的东西.我要更改什么才能使链接正常工作?
侦察兵
<p><%= button_to "添加新侦察员", new_scout_path, :method =>:get%></p><div class="留言板"><表格><tr><th>姓名</th><th>排名</th>提前日期</th><th>年龄</th></tr><% @scouts.each 做 |scout|%><tr <% link_to edit_scout_path(scout) %>><td><%= scout.name %></td><td><%= scout.rank %</td><td><%=scout.advancement%></td><td><%=scout.age%></td></tr><%结束%> 解决方案 正如 Robin 所说,这是无效的 HTML.你可能不应该这样做.
我个人会使用 jQuery 在 tr
上放置一个 onclick
事件.tr
元素看起来像这样:
<tr data-link="<%= edit_scout_path(scout) %>">...</tr>
然后关联的 JavaScript(放置在诸如 app/assets/javascripts/scouts.js
之类的文件中)将是这样的:
$("tr[data-link]").click(function() {window.location = $(this).data("link")})
这将使所有具有 data-link
属性的 tr
元素以我认为可能的最不显眼的方式充当 URL.
I am trying to make a row in a table link to the edit page. I know the links are being created, because I can print them out. I am close, but am missing something important. What do I change to make the link work properly?
<h1>Scouts</h1>
<p><%= button_to "Add a new Scout", new_scout_path, :method => :get %></p>
<div class="message-board">
<table>
<tr>
<th>Name</th>
<th>Rank</th>
<th>Advancement Date</th>
<th>Age</th>
</tr>
<% @scouts.each do |scout| %>
<tr <% link_to edit_scout_path(scout) %> >
<td><%= scout.name %></td>
<td><%= scout.rank %></td>
<td><%= scout.advancement %></td>
<td><%= scout.age %></td>
</tr>
<% end %>
</table>
</div>
解决方案 As Robin said, that's invalid HTML. You probably shouldn't do that.
I personally would put an onclick
event on the tr
using jQuery. The tr
element would look like this:
<tr data-link="<%= edit_scout_path(scout) %>">
...
</tr>
And then the associated JavaScript (placed in a file such as app/assets/javascripts/scouts.js
) would be something like this:
$("tr[data-link]").click(function() {
window.location = $(this).data("link")
})
This would make all tr
elements that have a data-link
attribute act as if they were URLs in the most unobtrusive way I can think possible.
这篇关于在 Rails 中将表格行变成链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文
登录
关闭
扫码关注1秒登录
发送“验证码”获取
|
15天全站免登陆