从gem继承控制器类

时间:2016-04-21 14:28:19

标签: ruby-on-rails rubygems shopify

我正在使用ShopifyApp gem,其receive中有一个名为WebhooksController的动作。如下所示: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'

3 个答案:

答案 0 :(得分:2)

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

我让他们知道并将我的版本从4.2.5提升到4.2.6现在它可以工作..令人沮丧但已经解决了...感谢大家的支持!

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

答案 1 :(得分:0)

尝试按此顺序放置路线:

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

答案 2 :(得分:0)

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.

So in order for your own route to take precedence over the one that is defined in the ShopifyApp gem, you need to define it before mounting the ShopifyApp engine.

Moving your own route post '/webhooks/receive', to: 'webhooks#receive' before mount ShopifyApp::Engine, at: '/' should fix the issue.

Refer to Rails Routing from the Outside In for more info.