2013-06-01 72 views
0

在我們的Rails應用程序中,我有幾個webhook,由外部服務(即PayPal IPN,Mailgun和Pusher)調用。目前,每個類型都有它自己的控制器+必要的途徑,例如:組織這段代碼的慣用方式是「rails方式」

post 'jobs/:job_id/comments/reply' => 'mailgun#incoming_email_comment' 
post '/webhook' => 'pusher#webhook' 
post '/paypal/ipn', :to => 'paypal#ipn', :as => :paypal_ipn 

是否有一個更清潔,更「軌道的方式」來實現這一目標?

回答

0

例如post '/webhook' => 'pusher#webhook'

pushers_controller.rb

class Pushers <ApplicationController 
    def webhook 
    .... 
    end 
end 

的routes.rb

resources :pushers do 
    collection do 
    post 'webhook' 
    end 
end 

現在你可以得到的URL像這樣

webhook_pushers_path 

這條路線或者

link_to 'some text', webhook_pushers_path 

正如你所看到的,現在你不需要手工編寫url。 Rails使用您的控制器的名稱和您的操作自動創建它。 您可以在那裏閱讀更多http://guides.rubyonrails.org/routing.html#adding-more-restful-actions