【问题标题】:Generate a `link_to` to the controller action `edit`, dynamically动态生成到控制器操作“edit”的“link_to”
【发布时间】:2011-07-15 11:38:01
【问题描述】:

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

所以我不能使用“神奇的RoR方式”路线

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

我想做如下的东西:

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

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

【问题讨论】:

  • 您想在控制器操作中使用link_to

标签: ruby-on-rails ruby ruby-on-rails-3 dynamic link-to


【解决方案1】:

您可以像这样使用“数组样式”编写路由:

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

【讨论】:

    【解决方案2】:

    有一个 edit_polymorphic_url 和 (edit_polymorphic_path) 助手可用: https://github.com/rails/.../polymorphic_routes.rb#L32

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多