生成一个`link_to`到控制器动作`edit` [英] Generate a `link_to` to the controller action `edit`, dynamically

查看:155
本文介绍了生成一个`link_to`到控制器动作`edit`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ruby on Rails 3.0.7,我想生成一个 link_to 到控制器动作编辑动态。我必须在部分模板中使用它,但问题是我为不同的模型数据渲染相同的部分模板(也就是说,我传递不同类实例的局部变量)。



所以我不能使用路线魔法RoR路

 `edit_< singular_name_of_the_resource> _path(< ; resource_class_instance>)`。 

我想制作如下内容:

  link_to(@resource_class_instance,:action =>'edit')#这个例子是错误的,但它建议的想法是
/ pre>

是可能吗?如果是这样,我该怎么做?

解决方案

您可以使用array style :

  = link_toEdit,[:edit,@your_resource] 
/ pre>

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?

解决方案

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

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

这篇关于生成一个`link_to`到控制器动作`edit`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆