简体   繁体   English

从 gem 继承控制器类

[英]Inherit controller class from gem

I am using the ShopifyApp gem, which has an action called receive in its WebhooksController .我正在使用ShopifyApp gem,它在其WebhooksController有一个名为receive的操作。 As seen here: Webhooks controller如下所示: Webhooks 控制器

In my WebhooksController controller I am trying to override that receive action by doing the following:在我的WebhooksController控制器中,我试图通过执行以下操作来覆盖该receive操作:

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

My route to my WebhooksController is this:我到WebhooksController路线是这样的:

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

And the route put in by the Gem engine is:而 Gem 引擎放入的路由是:

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

I see the data come in, but for some reason its not hitting my receive action and stopping at my pry and I'm not sure why.我看到数据进来了,但由于某种原因,它没有达到我的receive动作并停止我的pry ,我不知道为什么。

Found this and tried it, but no luck. 找到了这个并尝试了它,但没有运气。

I tried this solution too and it didn't work.. 我也试过这个解决方案,但没有用。

Any thoughts?有什么想法吗?

Here are the top of my logs showing what's happening:以下是我的日志顶部,显示了正在发生的事情:

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?'

And my routes file还有我的路线文件

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'

After two days of troubleshooting I realized their Gemspec was including an old version of rails that did not contain the method they were calling..经过两天的故障排除后,我意识到他们的 Gemspec 包含一个旧版本的 rails,其中不包含他们正在调用的方法。

I let them know and bumped my version from 4.2.5 to 4.2.6 and now it works.. frustrating but solved.. thanks everyone for your support!我让他们知道并将我的版本从 4.2.5 提升到 4.2.6,现在它可以工作了..令人沮丧但解决了..感谢大家的支持!

If anyone else comes across this, in order to use the Shopify App gem Webhooks controller you will need to be running Rails 4.2.6.如果其他人遇到此问题,为了使用 Shopify App gem Webhooks 控制器,您需要运行 Rails 4.2.6。

Try putting routes in this order:尝试按以下顺序放置路由:

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. Rails 路由按照指定的顺序进行匹配,因此如果您在 get 'photos/poll' 上方有一个 resources :photos,则资源行的 show action 路由将在 get 行之前匹配。 To fix this, move the get line above the resources line so that it is matched first.要解决此问题,请将 get 行移动到资源行上方,使其首先匹配。

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.因此,为了让您自己的路线优先于ShopifyApp gem 中定义的ShopifyApp ,您需要在安装ShopifyApp引擎之前定义它。

Moving your own route post '/webhooks/receive', to: 'webhooks#receive' before mount ShopifyApp::Engine, at: '/' should fix the issue.mount ShopifyApp::Engine, at: '/'之前post '/webhooks/receive', to: 'webhooks#receive'将您自己的路由post '/webhooks/receive', to: 'webhooks#receive' mount ShopifyApp::Engine, at: '/'应该可以解决该问题。

Refer to Rails Routing from the Outside In for more info.有关更多信息,请参阅从外向内的 Rails 路由

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM