Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a table row into a link in Rails

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> 
like image 214
jhamm Avatar asked Mar 30 '12 15:03

jhamm


1 Answers

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.

like image 190
Ryan Bigg Avatar answered Sep 21 '22 13:09

Ryan Bigg