【问题标题】:Rails link_to helperRails link_to 助手
【发布时间】:2014-05-02 01:53:23
【问题描述】:

我的 Rails 应用程序中有以下路线:

recipient_template DELETE /templates/:id/recipient/:recipient_type/:recipient_id(.:format) 

鉴于收件人类型和收件人 ID 均由我的应用程序动态填充,我如何在我的视图中创建指向此特定路由的链接。

编辑

这是我的模型和 route.rb 文件

class Template < ActiveRecord::Base
     has_and_belongs_to_many :individuals
     has_and_belongs_to_many :groups

     def recipients
         {:groups => self.groups, :individuals => self.individuals}
     end
end

resources :templates do
    member do
        match 'recipient/:recipient_type/:recipient_id', :to => :recipient_remove, :via => :delete
    end
end

【问题讨论】:

  • 你试过运行rake routes 吗?它应该给出答案!
  • link_to('Template', recipient_template_path(recipient_type: recipient_type,recipient_type: recipient_type )) 可能工作...
  • @xlembouras 我上面提供的是表单 rake 路由。我在这里想要实现的是从模型模板中删除特定的收件人。收件人可以是团体或个人,因此我必须指定所选收件人的类型和 ID。
  • @ArupRakshit 我试过了,结果是:Started DELETE "/templates/1/recipient?recipient_id=id&recipient_type=type"
  • @ArupRakshit 那行不通。还有你为什么要通过recipient_type 两次。

标签: ruby-on-rails ruby


【解决方案1】:

试试这个

在路径中传递接收者类型和接收者 ID 值以及模板 ID

link_to "Delete Recipient", recipient_remove_path(template.id, recipient_type, recipient_id), method: :delete

在你的路线中

match 'recipient/:recipient_type/:recipient_id' => "controller#action", :as => :recipient_remove, :via => :delete

并且您应该在接收者类型中传递接收者类名,并在接收者 ID 中传递接收者的 ID

【讨论】:

  • Anil - 如何确定recipient_typerecipient_id 的值?
  • 我试过这个并且:开始删除“/templates/1/recipient.Group”,我收到一个路由错误,说没有路由匹配[DELETE]“/templates/1/recipient.Group”跨度>
  • @mdoust 显示routes.rb 内容.. 以及视图... 这会很有帮助.. 我们可以帮助您..
  • @KirtiThorat 它有什么帮助?为什么在这种情况下需要method: :delete 的参数?
  • @mdoust 如果您收到Started DELETE "/templates/1/recipient.Group" 作为请求,那么这仅意味着您在routes.rb 中定义了另一个路由,其中​​DELETE 动词定义在recipient_template 之前。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-02
  • 2013-05-13
  • 1970-01-01
  • 1970-01-01
  • 2011-11-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多