7

我需要将以下成员方法添加到一些资源中,有没有办法把它干掉?

 member do
    get   :votes
    post  :up_vote
    post  :down_vote
  end

在我的 routes.rb

resources :news do
  resources :comments do
     member do
        get   :votes
        post  :up_vote
        post  :down_vote
      end
  end
end

resources :downloads do
  resources :whatever do
     member do
        get   :votes
        post  :up_vote
        post  :down_vote
      end
  end
end

编辑

我实际上已经把它移到了一个模块中,如下所示:

module Votable
  module RoutingMethods
    def votable_resources
      member do
        get   "up_votes"
        get   "down_votes"
        post  "up_vote"
        post  "down_vote"
      end
    end
  end # RoutingMethods
end

现在,我的 routes.rb 看起来像这样:

require 'votable'

include Votable::RoutingMethods

MyApp::Application.routes.draw do

  namespace :main, :path => "/" do
    resources :users do 
      resources :comments do
        votable_resources
      end
    end
  end

end

请参阅我的内联注释,但我希望命名路由看起来像:main_users_comments_up_votes

4

1 回答 1

7

你不能在你的路由文件中定义一个方法吗?

def foo
  member do
   get   :votes
   post  :up_vote
   post  :down_vote
  end
end


resources :news do
 resources :comments do
   foo
 end
end

编辑

我以前没有使用过这种技术。当我做'rake routes'时,它似乎起作用了。无论如何,路由文件只是 Ruby 代码。请注意您定义的方法的名称,因为它是在 ActionDispatch::Routing::Mapper 的实例中定义的。

# routes.rb

MyApp::Application.routes.draw do
  puts self
  puts y self.methods.sort
end

# Terminal
> rake routes
于 2010-11-24T03:09:11.037 回答