link_to with double namespace

时间:2017-05-08 17:37:01

标签: ruby-on-rails ruby

我有这样定义的路线:

 namespace :dealer, path: '' do
   resources :images, except: :index do
     resources :comments, only: [:create, :destroy] do
       resources :attachments, only: :show
     end
   end
 end

在我的视图中,使用link_to url_for([@image, comment])会生成名为“

”的网址

dealer_image_dealer_comment_path不存在。它应该是dealer_image_comment_path。有什么方法可以避免双重命名空间?

我收到此错误:

undefined method `dealer_image_dealer_comment_path' for #<#<Class:0x007fb599499520>:0x007fb5994980d0>
Did you mean?  dealer_image_comments_path
               dealer_image_comment_path

rake routes:

dealer_image_comments POST      /images/:image_id/comments(.:format)                                                                   dealer/comments#create {:subdomain=>"dealer"}
dealer_image_comment DELETE    /images/:image_id/comments/:id(.:format)                                                               dealer/comments#destroy {:subdomain=>"dealer"}

2 个答案:

答案 0 :(得分:0)

据我所知,使用link_to,您可以执行以下操作:

link_to "Comment", [@image, comment]

查看link_to UrlHelper了解更多信息

答案 1 :(得分:0)

我最终写了自己的帮助来完成这个:

  def path_gen(namespace, models)
    path = ''
    models.compact!.each do |m|
      path = path + '_' + m.class.name.demodulize.downcase
    end
    send("#{namespace}#{path}_path", *models)
  end

接受path_gen([@image, comment])