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

戏子无情 提交于 2019-12-21 01:06:15

问题


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?


回答1:


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

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



回答2:


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



来源:https://stackoverflow.com/questions/6706448/generate-a-link-to-to-the-controller-action-edit-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!