ruby-on-rails – Rails 3路由:DRY成员

我需要将以下成员方法添加到许多资源中,有没有办法干掉它?
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

解决方法

你不能只在路线文件中定义一个方法吗?
def foo
  member do
   get   :Votes
   post  :up_Vote
   post  :down_Vote
  end
end


resources :news do
 resources :comments do
   foo
 end
end

编辑

我之前没有使用过这种技术.当我做“耙路线”时,它似乎工作.无论如何,routes文件只是Ruby代码.请注意您定义的方法名称,因为它是在Actiondispatch :: Routing :: Mapper的实例中定义的.

# routes.rb

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

# Terminal
> rake routes

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...