Rails 设计引擎 Sign_out 链接错误
Posted
技术标签:
【中文标题】Rails 设计引擎 Sign_out 链接错误【英文标题】:Rails Devise Engine Sign_out link Error 【发布时间】:2014-10-27 19:45:23 【问题描述】:我已经在我的引擎上安装了设计,并在我的 application.html.erb 文件中应用了以下内容:
<div id="user_nav">
<% if user_signed_in? %>
Signed in as <%= current_user.email %>. This cannot be cheese?
<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
<% else %>
<%= link_to 'Register', new_user_registration_path %> or <%= link_to 'Sign in', new_user_session_path %>
<% end %>
</div>
它显示No route matches "/users/sign_out"
并显示错误
<%= link_to 'Register', new_user_registration_path %> or <%= link_to 'Sign in', new_user_session_path %>
错误消息NameError in Devise::Sessions#new
undefined local variable or method `destroy_user_session_path' for #<#<Class:0x007fb4da2e9618>:0x007fb4da2db9a0>
Rake 路由结果
Routes for Fd::Engine:
users_auth_google_oauth2_callback GET /users/auth/google_oauth2/callback(.:format) fd/omniauth_callbacks#google_oauth2
fd_test_index GET /fd/test/index(.:format) fd/fd/test#index
fd_omniauth_callbacks_google_oauth2 GET /fd/omniauth_callbacks/google_oauth2(.:format) fd/fd/omniauth_callbacks#google_oauth2
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
user_omniauth_authorize GET|POST /users/auth/:provider(.:format) omniauth_callbacks#passthru :provider=>/google_oauth2/
user_omniauth_callback GET|POST /users/auth/:action/callback(.:format) omniauth_callbacks#(?-mix:google_oauth2)
fd_auth_index GET /fd/auth/index(.:format) fd/fd/auth#index
root GET / fd/auth#index
omniauth_callbacks_verify_otp GET /omniauth_callbacks/verify_otp(.:format) fd/omniauth_callbacks#verify_otp
omniauth_callbacks_delete_session GET /omniauth_callbacks/delete_session(.:format) fd/omniauth_callbacks#sign_out
引擎路线:
Fd::Engine.routes.draw do
devise_for :users,:controllers => :omniauth_callbacks => "omniauth_callbacks" , :class_name => "Fd::User", module: :devise
end
【问题讨论】:
虽然rake routes
显示了确切的路径,但仍然出现错误并且它指向 Devise::Sessions#new
类,因为不应指向新的会话
问题出现在<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
行,而不是您发布的内容。不过一切看起来都很好。
试试这个 退出
GhostBuster ,它不工作显示相同的错误。我在引擎中安装了设计,所以是否需要任何额外的路由或配置?
【参考方案1】:
您在设计中集成了 OmniAuth,因此如果用户提供正确的凭据,设计将创建会话,根据您的问题,我了解会话已成功创建,因此如果您想要清除会话,只需在控制器中创建一个操作(您开始会话的位置)并给出
redirect_to "/users/sign_out"
或
试试
redirect_to destroy_user_session_path
我希望这会奏效
【讨论】:
【参考方案2】:试试这个:
<%= link_to "Sign out", destroy_user_session_path, :method => :get %>
并确保您的config/initializers/devise.rb
中有以下行
config.sign_out_via = :get
【讨论】:
它显示相同的错误“未定义的局部变量或方法`destroy_user_session_path'...” 并且还要确保你已经在 routes.rb 中定义了 devise_for :users 我试过但结果相同。 new_user_registration 也不起作用。设计是否需要任何特殊配置(嵌入在引擎中)以上是关于Rails 设计引擎 Sign_out 链接错误的主要内容,如果未能解决你的问题,请参考以下文章
没有路线匹配 [GET] "/users/sign_out" rails 5
没有路线匹配 [GET] "/users/sign_out" **Rails 5/Bootstrap 4**
Rails 3.0.9 + Devise + Cucumber + Capybara 臭名昭著的“没有路线匹配 /users/sign_out”