Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate a `link_to` to the controller action `edit`, dynamically

I am using Ruby on Rails 3.0.7 and I would like to generate a link_to to the controller action edit, dynamically. I have to use it in a partial template but the issue is that I am rendering that same partial template for different model data (that is, I pass local variables of different class instances in that).

So I can not use the route "magical RoR way"

`edit_<singular_name_of_the_resource>_path(<resource_class_instance>)`.

I would like to make something like the following:

link_to( @resource_class_instance, :action => 'edit') # This example is wrong, but it suggests the idea

Is it possible? If so, how can I do that?

like image 689
Backo Avatar asked Jul 15 '11 11:07

Backo


2 Answers

You can write routes using the "array style" like this :

= link_to "Edit", [:edit, @your_resource]
like image 200
Nicolas Blanco Avatar answered Nov 14 '22 05:11

Nicolas Blanco


There is a edit_polymorphic_url and (edit_polymorphic_path) helper available: https://github.com/rails/.../polymorphic_routes.rb#L32

like image 21
Mario Uher Avatar answered Nov 14 '22 05:11

Mario Uher