Ruby Forum Ruby on Rails > make a <tr>clickable

Posted by Richard Schneeman (snowmaninthesun)
on 01.04.2009 06:39
I would like to make an entire table row clickable so the user will be
directed to another url. If i was using hand written javascript i would
just add an onclick to the <tr>, but i want to use rails routes and to
be able to pass in parameters from ruby. What is the best way to
accomplish this?

<tr onclick = <%= link_to my_route_path %> > <td>content</td></tr>
Posted by Daniel Liebig (dli)
on 01.04.2009 10:00
Richard Schneeman wrote:
> I would like to make an entire table row clickable so the user will be
> directed to another url. If i was using hand written javascript i would
> just add an onclick to the <tr>, but i want to use rails routes and to
> be able to pass in parameters from ruby. What is the best way to
> accomplish this?
> 
> <tr onclick = <%= link_to my_route_path %> > <td>content</td></tr>

onclick="window.location.href = '<%= url_for :controller => ':foo' 
:action => 'bar' %>'

see: http://api.rubyonrails.org/
see: 
http://api.rubyonrails.org/classes/ActionController/Base.html#M000624

D.
Posted by HeChian (Guest)
on 01.04.2009 10:28
(Received via mailing list)
I think you can make it done by jQuery or Prototype with CSS Selector:

<tr class="clickable" rel="<%= my_route_path %>"><td>content</td></tr>

<script>
(function($){
$('tr.clickable').click(function(){ // You can use .live('click',
function(){}) if you use jQuery 1.3.1 or higher
  // Link to ...
});
})(jQuery);
</script>

On 4月1日, 下午12時39分, Richard Schneeman <rails-mailing-l...@andreas-
Posted by Richard Schneeman (snowmaninthesun)
on 09.04.2009 06:06
Thanks, both work great! Though i guess neither would degrade well with 
javascript turned off. Guess you cant have your clickable-rows and eat 
them too ^_^