Backo
Backo

Reputation: 18871

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?

Upvotes: 6

Views: 6868

Answers (2)

Nicolas Blanco
Nicolas Blanco

Reputation: 11299

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

= link_to "Edit", [:edit, @your_resource]

Upvotes: 12

Mario Uher
Mario Uher

Reputation: 12397

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

Upvotes: 1

Related Questions