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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With