我无法使用像user_signed_in这样的设计助手?或任何其他有此宝石的人。我的堆栈跟踪和错误如下。有没有人有工作的解决方案?我之前从未遇到过这个问题。我正在开展一个大型项目,我需要将API包含在移动设备的整体应用基础架构中。我注释掉了设计身份验证令牌路由,看看它们是否是导致问题的原因。无济于事,我现在无法对项目本身做任何事情;由于被锁定在Devise路线之外。
wrong number of arguments (given 1, expected 0)
<% if user_signed_in? %>
devise (4.3.0) app/controllers/devise_controller.rb:47:in resource_class' devise_token_auth (0.1.42) app/controllers/devise_token_auth/concerns/set_user_by_token.rb:21:inset_user_by_token'
devise_token_auth (0.1.42) lib/devise_token_auth/controllers/helpers.rb:123:in current_user' devise_token_auth (0.1.42) lib/devise_token_auth/controllers/helpers.rb:119:inuser_signed_in?'
actionpack (5.1.1) lib/abstract_controller/helpers.rb:68:in user_signed_in?' app/views/layouts/application.html.erb:16:in_app_views_layouts_application_html_erb___51327025_116383452'
actionview (5.1.1) lib/action_view/template.rb:157:in block in render' activesupport (5.1.1) lib/active_support/notifications.rb:168:ininstrument'
actionview (5.1.1) lib/action_view/template.rb:352:in instrument_render_template' actionview (5.1.1) lib/action_view/template.rb:155:inrender'
actionview (5.1.1) lib/action_view/renderer/template_renderer.rb:64:in render_with_layout' actionview (5.1.1) lib/action_view/renderer/template_renderer.rb:50:inrender_template'
actionview (5.1.1) lib/action_view/renderer/template_renderer.rb:14:in render' actionview (5.1.1) lib/action_view/renderer/renderer.rb:42:inrender_template'
actionview (5.1.1) lib/action_view/renderer/renderer.rb:23:in render' actionview (5.1.1) lib/action_view/rendering.rb:103:in_render_template'
actionpack (5.1.1) lib/action_controller/metal/streaming.rb:217:in _render_template' actionview (5.1.1) lib/action_view/rendering.rb:83:inrender_to_body'
actionpack (5.1.1) lib/action_controller/metal/rendering.rb:52:in render_to_body' actionpack (5.1.1) lib/action_controller/metal/renderers.rb:141:inrender_to_body'
actionpack (5.1.1) lib/abstract_controller/rendering.rb:24:in render' actionpack (5.1.1) lib/action_controller/metal/rendering.rb:36:inrender'
actionpack (5.1.1) lib/action_controller/metal/instrumentation.rb:44:in block (2 levels) in render' activesupport (5.1.1) lib/active_support/core_ext/benchmark.rb:12:inblock in ms'
C:/Ruby233/lib/ruby/2.3.0/benchmark.rb:308:in realtime' activesupport (5.1.1) lib/active_support/core_ext/benchmark.rb:12:inms'
actionpack (5.1.1) lib/action_controller/metal/instrumentation.rb:44:in block in render' actionpack (5.1.1) lib/action_controller/metal/instrumentation.rb:87:incleanup_view_runtime'
activerecord (5.1.1) lib/active_record/railties/controller_runtime.rb:29:in cleanup_view_runtime' actionpack (5.1.1) lib/action_controller/metal/instrumentation.rb:43:inrender'
responders (2.4.0) lib/action_controller/responder.rb:238:in default_render' responders (2.4.0) lib/action_controller/responder.rb:170:into_html'
responders (2.4.0) lib/action_controller/responder.rb:163:in respond' responders (2.4.0) lib/action_controller/responder.rb:156:incall'
responders (2.4.0) lib/action_controller/respond_with.rb:211:in respond_with' devise (4.3.0) app/controllers/devise/sessions_controller.rb:12:innew'
actionpack (5.1.1) lib/action_controller/metal/basic_implicit_render.rb:4:in send_action' actionpack (5.1.1) lib/abstract_controller/base.rb:186:inprocess_action'
actionpack (5.1.1) lib/action_controller/metal/rendering.rb:30:in process_action' actionpack (5.1.1) lib/abstract_controller/callbacks.rb:20:inblock in process_action'
activesupport (5.1.1) lib/active_support/callbacks.rb:131:in run_callbacks' actionpack (5.1.1) lib/abstract_controller/callbacks.rb:19:inprocess_action'
actionpack (5.1.1) lib/action_controller/metal/rescue.rb:20:in process_action' actionpack (5.1.1) lib/action_controller/metal/instrumentation.rb:32:inblock in process_action'
activesupport (5.1.1) lib/active_support/notifications.rb:166:in block in instrument' activesupport (5.1.1) lib/active_support/notifications/instrumenter.rb:21:ininstrument'
activesupport (5.1.1) lib/active_support/notifications.rb:166:in instrument' actionpack (5.1.1) lib/action_controller/metal/instrumentation.rb:30:inprocess_action'
actionpack (5.1.1) lib/action_controller/metal/params_wrapper.rb:252:in process_action' activerecord (5.1.1) lib/active_record/railties/controller_runtime.rb:22:inprocess_action'
actionpack (5.1.1) lib/abstract_controller/base.rb:124:in process' actionview (5.1.1) lib/action_view/rendering.rb:30:inprocess'
actionpack (5.1.1) lib/action_controller/metal.rb:189:in dispatch' actionpack (5.1.1) lib/action_controller/metal.rb:253:indispatch'
actionpack (5.1.1) lib/action_dispatch/routing/route_set.rb:49:in dispatch' actionpack (5.1.1) lib/action_dispatch/routing/route_set.rb:31:inserve'
actionpack (5.1.1) lib/action_dispatch/routing/mapper.rb:16:in block in <class:Constraints>' actionpack (5.1.1) lib/action_dispatch/routing/mapper.rb:46:inserve'
actionpack (5.1.1) lib/action_dispatch/journey/router.rb:46:in block in serve' actionpack (5.1.1) lib/action_dispatch/journey/router.rb:33:ineach'
actionpack (5.1.1) lib/action_dispatch/journey/router.rb:33:in serve' actionpack (5.1.1) lib/action_dispatch/routing/route_set.rb:832:incall'
warden (1.2.7) lib/warden/manager.rb:36:in block in call' warden (1.2.7) lib/warden/manager.rb:35:incatch'
warden (1.2.7) lib/warden/manager.rb:35:in call' rack (2.0.3) lib/rack/etag.rb:25:incall'
rack (2.0.3) lib/rack/conditional_get.rb:25:in call' rack (2.0.3) lib/rack/head.rb:12:incall'
rack (2.0.3) lib/rack/session/abstract/id.rb:232:in context' rack (2.0.3) lib/rack/session/abstract/id.rb:226:incall'
actionpack (5.1.1) lib/action_dispatch/middleware/cookies.rb:613:in call' activerecord (5.1.1) lib/active_record/migration.rb:556:incall'
actionpack (5.1.1) lib/action_dispatch/middleware/callbacks.rb:26:in block in call' activesupport (5.1.1) lib/active_support/callbacks.rb:97:inrun_callbacks'
actionpack (5.1.1) lib/action_dispatch/middleware/callbacks.rb:24:in call' actionpack (5.1.1) lib/action_dispatch/middleware/executor.rb:12:incall'
actionpack (5.1.1) lib/action_dispatch/middleware/debug_exceptions.rb:59:in call' web-console (3.5.1) lib/web_console/middleware.rb:135:incall_app'
web-console (3.5.1) lib/web_console/middleware.rb:28:in block in call' web-console (3.5.1) lib/web_console/middleware.rb:18:incatch'
web-console (3.5.1) lib/web_console/middleware.rb:18:in call' actionpack (5.1.1) lib/action_dispatch/middleware/show_exceptions.rb:31:incall'
railties (5.1.1) lib/rails/rack/logger.rb:36:in call_app' railties (5.1.1) lib/rails/rack/logger.rb:24:inblock in call'
activesupport (5.1.1) lib/active_support/tagged_logging.rb:69:in block in tagged' activesupport (5.1.1) lib/active_support/tagged_logging.rb:26:intagged'
activesupport (5.1.1) lib/active_support/tagged_logging.rb:69:in tagged' railties (5.1.1) lib/rails/rack/logger.rb:24:incall'
sprockets-rails (3.2.0) lib/sprockets/rails/quiet_assets.rb:13:in call' actionpack (5.1.1) lib/action_dispatch/middleware/remote_ip.rb:79:incall'
actionpack (5.1.1) lib/action_dispatch/middleware/request_id.rb:25:in call' rack (2.0.3) lib/rack/method_override.rb:22:incall'
rack (2.0.3) lib/rack/runtime.rb:22:in call' activesupport (5.1.1) lib/active_support/cache/strategy/local_cache_middleware.rb:27:incall'
actionpack (5.1.1) lib/action_dispatch/middleware/executor.rb:12:in call' actionpack (5.1.1) lib/action_dispatch/middleware/static.rb:125:incall'
rack (2.0.3) lib/rack/sendfile.rb:111:in call' railties (5.1.1) lib/rails/engine.rb:522:incall'
puma (3.8.2) lib/puma/configuration.rb:224:in call' puma (3.8.2) lib/puma/server.rb:600:inhandle_request'
puma (3.8.2) lib/puma/server.rb:435:in process_client' puma (3.8.2) lib/puma/server.rb:299:inblock in run'
puma (3.8.2) lib/puma/thread_pool.rb:120:in `block in spawn_thread'
Application_Controller.rb
class ApplicationController < ActionController::Base
include DeviseTokenAuth::Concerns::SetUserByToken
protect_from_forgery with: :exception
include Pundit
class User::ParameterSanitizer < Devise::ParameterSanitizer
def initialize(*)
super
permit(:sign_up, keys: [:username, :email, :first_name, :last_name, :country, :state, :city_or_town, :zip_code, :how_did_you_learn_about_us])
end
end
class Employer::ParameterSanitizer < Devise::ParameterSanitizer
def initialize(*)
super
permit(:sign_up, keys: [:username, :email, :company_name, :full_name, :contact_number, :employee_count])
end
end
protected
def devise_parameter_sanitizer
if resource_class == User
User::ParameterSanitizer.new(User, :user, params)
elsif resource_class == Employer
Employer::ParameterSanitizer.new(Employer, :employer, params)
else
super
end
end
end
的routes.rb
Rails.application.routes.draw do
mount CountryStateSelect::Rails::Engine, at: "/"
devise_for :admins
devise_for :employers
devise_for :users
namespace :api do
scope :v1 do
mount_devise_token_auth_for 'User', at: 'user_auth'
mount_devise_token_auth_for 'Employer', at: 'employer_auth'
end
end
devise_scope :user do
authenticated do
root to: 'user_dashboard#index', as: 'authenticated_user_root'
end
unauthenticated do
root to: 'home#index', as: 'unauthenticated_user_root'
end
end
devise_scope :employer do
authenticated do
root to: 'employer_dashboard#index', as: 'authenticated_employer_root'
end
unauthenticated do
root to: 'home#index', as: 'unauthenticated_employer_root'
end
end
devise_scope :admin do
authenticated do
root to: 'admin_dashboard#admin', as: 'authenticated_admin_root'
end
unauthenticated do
root to: 'home#index', as: 'unauthenticated_admin_root'
end
end
Application.html.erb
<body>
<% if user_signed_in? %>
<% elsif employer_signed_in? %>
<% elsif admin_signed_in? %>
<% else %>
// Default Route
</body>
devise_token_auth.rb
DeviseTokenAuth.setup do |config|
config.enable_standard_devise_support = true
end
答案 0 :(得分:0)
如果您以前使用过bcrypt,请确保删除在应用程序控制器(current_user,login,auth)中手动创建的某些方法,以确保它与设计提供的方法不冲突。
检查会话控制器内部,您的进程/运行登录用户方法如何,下面是为登录用户创建会话的示例代码
class SessionsController < ApplicationController
def create
user = User.find_by_username(params[:session][:username])
if user && user.authenticate(params[:session][:password])
login user
end
end
...
end
答案 1 :(得分:0)
避免将devise_token_auth与设计基础gem冲突。需要对您的API进行容器化,并使其远离普通控制器。
例如,下面:
# controllers/api/v1/application_controller.rb
module Api
module V1
class ApplicationController < ApplicationController
skip_before_action :verify_authenticity_token
include DeviseTokenAuth::Concerns::SetUserByToken
end
end
end
API控制器包含DeviseTokenAuth关注点,用于在自己的域中管理应用程序。它派生自原始的ApplicationController。
现在,在原始的Application Controller中。您只需验证是否正在使用API控制器。
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception, if: :verify_api
def verify_api
params[:controller].split('/')[0] != 'devise_token_auth'
end
end
整个想法是重定向应用程序的每个流,具体取决于所需的基础架构部分。我希望这个解决方案能够贯彻始终。我也希望宝石的制造商能够采取更稳定的方法。