Rails 3 Routes: DRY members

前端 未结 1 1584
-上瘾入骨i
-上瘾入骨i 2021-01-02 18:19

I need to add the following member methods to a number of resources, is there a way to DRY this up?

 member do
    get   :votes
    post  :up_vote
    post           


        
相关标签:
1条回答
  • 2021-01-02 19:03

    Can't you just define a method in your routes files?

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

    Edit

    I haven't used this technique before. It just seemed to work when I did 'rake routes'. Anyways, the routes file is just Ruby code. Just be careful about the name of the method you define because it's defined in the instance of ActionDispatch::Routing::Mapper.

    # routes.rb
    
    MyApp::Application.routes.draw do
      puts self
      puts y self.methods.sort
    end
    
    # Terminal
    > rake routes
    
    0 讨论(0)
提交回复
热议问题