从 gem 继承控制器 class

Inherit controller class from gem

我正在使用 ShopifyApp gem,它的 WebhooksController 中有一个名为 receive 的操作。如此处所示:Webhooks controller

在我的 WebhooksController 控制器中,我试图通过执行以下操作来覆盖 receive 操作:

class WebhooksController < ShopifyApp::WebhooksController
  def receive
    binding.pry
  end
end

我去 WebhooksController 的路线是这样的:

webhooks_receive POST   /webhooks/receive(.:format)  webhooks#receive

而Gem引擎输入的路由是:

webhooks POST /webhooks/:type(.:format)  shopify_app/webhooks#receive

我看到数据进来了,但出于某种原因,它没有达到我的 receive 操作并在我的 pry 处停止,我不确定为什么。

Found this and tried it, but no luck.

I tried this solution too and it didn't work..

有什么想法吗?

这是我日志的顶部,显示了正在发生的事情:

Started POST "/webhooks/receive" for XX.XXX.37.116 at 2016-04-21 14:57:02 
+0000
Cannot render console from XX.XXX.37.116! Allowed networks: XXX.0.0.1, ::1, 
127.0.0.0/127.XXX.XXX.255

ActiveRecord::SchemaMigration Load (0.2ms)  SELECT "schema_migrations".* 
FROM "schema_migrations"

Processing by ShopifyApp::WebhooksController#receive as */*
Parameters: {"rate"=>{"origin"=>{"country"=>"US", "postal_code"=>"48615",   
"province"=>"MI", "city"=>"Breckenridge", "name"=>nil, "address1"=>"6760.. 
bunch of data removed for brevity}}}

Completed 500 Internal Server Error in 6ms (ActiveRecord: 0.0ms)

NoMethodError (undefined method `variable_size_secure_compare' for   
ActiveSupport::SecurityUtils:Module):
shopify_app (7.0.2) lib/shopify_app/webhook_verification.rb:20:in   
`hmac_valid?'

还有我的路线文件

Rails.application.routes.draw do
root :to => 'home#index'
mount ShopifyApp::Engine, at: '/'
resources :store
resources :shipping_methods

post '/webhooks/receive', to: 'webhooks#receive'
post '/billing_plans', to: 'billing_plans#save_plan', as: 'save_plan'
get '/activate_charge', to: 'billing_plans#activate_charge', as: 'activate'
post '/create_charge', to: 'billing_plans#create_charge', as: 
'create_billing_plan'

尝试按以下顺序放置路由:

post '/webhooks/receive', to: 'webhooks#receive'
mount ShopifyApp::Engine, at: '/'

Rails routes are matched in the order they are specified, so if you have a resources :photos above a get 'photos/poll' the show action's route for the resources line will be matched before the get line. To fix this, move the get line above the resources line so that it is matched first.

因此,为了让您自己的路线优先于 ShopifyApp gem 中定义的路线,您需要在安装 ShopifyApp 引擎之前定义它。

mount ShopifyApp::Engine, at: '/' 之前移动您自己的路线 post '/webhooks/receive', to: 'webhooks#receive' 应该可以解决问题。

有关详细信息,请参阅 Rails Routing from the Outside In

经过两天的故障排除后,我意识到他们的 Gemspec 包含一个旧版本的 rails,但不包含他们正在调用的方法。

我让他们知道并将我的版本从 4.2.5 升级到 4.2.6,现在可以使用了。令人沮丧但已解决。感谢大家的支持!

如果其他人遇到此问题,为了使用 Shopify 应用程序 gem Webhooks 控制器,您需要 运行 Rails 4.2.6。