我正在尝试在rails引擎中使用shopify_app gem。首先,发电机似乎不支持发动机。它们与rails应用程序完美配合。所以,我用了
rails generate shopify_app --api_key [APIKEY_HERE] --secret [SECRET_HERE] --embedded false --scope read_content,read_products,read_customers,read_orders
在新的rails应用程序上并将更改复制到我的rails引擎中。我改变了名为Shopify的引擎如下; (一行后面的注释中的++表示添加了行,文件名中的++表示在初始生成引擎命令后添加了整个文件)
#app/controllers/shopify/application_controller.rb
module Shopify
class ApplicationController < ActionController::Base
include ShopifyApp::LoginProtection #++
protect_from_forgery with: :exception
end
end
#app/controllers/shopify/home_controller.rb # ++
module Shopify
class HomeController < ShopifyApp::AuthenticatedController
def index
@products = ShopifyAPI::Product.find(:all, :params => {:limit => 10})
end
end
end
# config/routes.rb
Shopify::Engine.routes.draw do
root :to => 'home#index' # ++
mount ShopifyApp::Engine, at: '/' # ++
end
# app/models/shop.rb (Using mongoid) # ++
module Shopify
class Shop
include Mongoid::Document
include ShopifyApp::Shop
include ShopifyApp::SessionStorage
end
end
# config/initializers/omniauth.rb # ++
Rails.application.config.middleware.use OmniAuth::Builder do
provider :shopify,
ShopifyApp.configuration.api_key,
ShopifyApp.configuration.secret,
scope: ShopifyApp.configuration.scope
end
# config/initializers/shopify_app.rb # ++
ShopifyApp.configure do |config|
config.api_key = "KEY_HERE"
config.secret = "SECRET_HERE"
config.scope = "read_content,read_products,read_customers,read_orders"
config.embedded_app = false
end
# config/initializers/shopify_session_repository.rb # ++
if Rails.configuration.cache_classes
ShopifyApp::SessionRepository.storage = Shopify::Shop
else
ActionDispatch::Reloader.to_prepare do
ShopifyApp::SessionRepository.storage = Shopify::Shop
end
end
# lib/shopify.rb (Not related to shopify_app, just a project file that loads shopify_app)
require "shopify/engine"
require "mongoid"
require "shopify_app" # ++
module Shopify
Mongoid.load!(Shopify::Engine.root.join("config", "mongoid.yml"))
end
<!--app/views/shopify/home/index.html.erb--> <!-- ++ -->
<h2>Products</h2>
<ul>
<% @products.each do |product| %>
<li><%= link_to product.title, "https://#{@shop_session.url}/admin/products/#{product.id}", target: "_top" %></li>
<% end %>
</ul>
我使用此命令创建新引擎rails plugin new shopify --mountable
当我尝试打开http://localhost:3000/shopify/时,我收到此错误:
NoMethodError (undefined method `login_again_if_different_shop' for #<Shopify::HomeController:0x007f8163d53540>):
activesupport (4.2.8) lib/active_support/callbacks.rb:432:in `block in make_lambda'
activesupport (4.2.8) lib/active_support/callbacks.rb:164:in `block in halting'
activesupport (4.2.8) lib/active_support/callbacks.rb:504:in `block in call'
activesupport (4.2.8) lib/active_support/callbacks.rb:504:in `each'
activesupport (4.2.8) lib/active_support/callbacks.rb:504:in `call'
activesupport (4.2.8) lib/active_support/callbacks.rb:92:in `__run_callbacks__'
activesupport (4.2.8) lib/active_support/callbacks.rb:778:in `_run_process_action_callbacks'
activesupport (4.2.8) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.8) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.8) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.8) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (4.2.8) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.8) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.8) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.8) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.8) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
actionpack (4.2.8) lib/abstract_controller/base.rb:137:in `process'
actionview (4.2.8) lib/action_view/rendering.rb:30:in `process'
actionpack (4.2.8) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.2.8) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.2.8) lib/action_controller/metal.rb:237:in `block in action'
actionpack (4.2.8) lib/action_dispatch/routing/route_set.rb:74:in `dispatch'
actionpack (4.2.8) lib/action_dispatch/routing/route_set.rb:43:in `serve'
actionpack (4.2.8) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.8) lib/action_dispatch/journey/router.rb:30:in `each'
actionpack (4.2.8) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.8) lib/action_dispatch/routing/route_set.rb:817:in `call'
railties (4.2.8) lib/rails/engine.rb:518:in `call'
railties (4.2.8) lib/rails/railtie.rb:194:in `public_send'
railties (4.2.8) lib/rails/railtie.rb:194:in `method_missing'
actionpack (4.2.8) lib/action_dispatch/routing/mapper.rb:51:in `serve'
actionpack (4.2.8) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.8) lib/action_dispatch/journey/router.rb:30:in `each'
actionpack (4.2.8) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.8) lib/action_dispatch/routing/route_set.rb:817:in `call'
omniauth (1.6.1) lib/omniauth/strategy.rb:189:in `call!'
omniauth (1.6.1) lib/omniauth/strategy.rb:167:in `call'
omniauth (1.6.1) lib/omniauth/builder.rb:63:in `call'
exception_notification (4.2.1) lib/exception_notification/rack.rb:32:in `call'
rack (1.6.8) lib/rack/etag.rb:24:in `call'
rack (1.6.8) lib/rack/conditionalget.rb:25:in `call'
rack (1.6.8) lib/rack/head.rb:13:in `call'
actionpack (4.2.8) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.8) lib/action_dispatch/middleware/flash.rb:260:in `call'
rack (1.6.8) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.8) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.2.8) lib/action_dispatch/middleware/cookies.rb:560:in `call'
actionpack (4.2.8) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.8) lib/active_support/callbacks.rb:88:in `__run_callbacks__'
activesupport (4.2.8) lib/active_support/callbacks.rb:778:in `_run_call_callbacks'
activesupport (4.2.8) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.8) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.8) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.8) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
actionpack (4.2.8) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.2.8) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.8) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.8) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.8) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.8) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.8) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.8) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.8) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.8) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.8) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.8) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.8) lib/rack/lock.rb:17:in `call'
actionpack (4.2.8) lib/action_dispatch/middleware/static.rb:120:in `call'
rack (1.6.8) lib/rack/sendfile.rb:113:in `call'
railties (4.2.8) lib/rails/engine.rb:518:in `call'
railties (4.2.8) lib/rails/application.rb:165:in `call'
rack (1.6.8) lib/rack/lock.rb:17:in `call'
rack (1.6.8) lib/rack/content_length.rb:15:in `call'
rack (1.6.8) lib/rack/handler/webrick.rb:88:in `service'
/Users/swarn/.rbenv/versions/2.3.1/lib/ruby/2.3.0/webrick/httpserver.rb:140:in `service'
/Users/swarn/.rbenv/versions/2.3.1/lib/ruby/2.3.0/webrick/httpserver.rb:96:in `run'
/Users/swarn/.rbenv/versions/2.3.1/lib/ruby/2.3.0/webrick/server.rb:296:in `block in start_thread'
您能否帮我解决这个问题或将我重定向到有人可以提供帮助的地方?感谢。