简体   繁体   English

Rails在devise中使用link_to的正确方法

[英]Rails correct way to use link_to with devise

I'm getting started with rails and devise for authentication and I want to make a link to sign out when a user is logged into the admin page. 我现在开始使用Rails并设计用于身份验证,我想创建一个链接以在用户登录到管理页面时注销。

What is the correct way to write the link_to code 编写link_to code的正确方法是什么

Here's my rake routes : 这是我的rake routes

admin_index        /admin/index(.:format)         {:controller=>"admin/home", :action=>"index"}
        new_user_session GET    /users/sign_in(.:format)       {:action=>"new", :controller=>"devise/sessions"}
            user_session POST   /users/sign_in(.:format)       {:action=>"create", :controller=>"devise/sessions"}
    destroy_user_session DELETE /users/sign_out(.:format)      {:action=>"destroy", :controller=>"devise/sessions"}
           user_password POST   /users/password(.:format)      {:action=>"create", :controller=>"devise/passwords"}
       new_user_password GET    /users/password/new(.:format)  {:action=>"new", :controller=>"devise/passwords"}
      edit_user_password GET    /users/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
                         PUT    /users/password(.:format)      {:action=>"update", :controller=>"devise/passwords"}
cancel_user_registration GET    /users/cancel(.:format)        {:action=>"cancel", :controller=>"devise/registrations"}
       user_registration POST   /users(.:format)               {:action=>"create", :controller=>"devise/registrations"}
   new_user_registration GET    /users/sign_up(.:format)       {:action=>"new", :controller=>"devise/registrations"}
  edit_user_registration GET    /users/edit(.:format)          {:action=>"edit", :controller=>"devise/registrations"}
                         PUT    /users(.:format)               {:action=>"update", :controller=>"devise/registrations"}
                         DELETE /users(.:format)               {:action=>"destroy", :controller=>"devise/registrations"}
              home_index GET    /home/index(.:format)          {:controller=>"home", :action=>"index"}
                    root        /                              {:controller=>"home", :action=>"index"}

I tried <%= link_to "Sign Out", destroy_user_session_path %> but when i click the link it gives me the error: 我试过<%= link_to "Sign Out", destroy_user_session_path %>但是当我单击链接时,它给了我错误:

No route matches [GET] "/users/sign_out"

From this devise sample application , recommended on the Devise wiki : 通过这个devise示例应用程序 ,在Devise Wiki上推荐:

<% if user_signed_in? %>
  <li><%= link_to 'Edit account', edit_user_registration_path %></li>
  <li><%= link_to 'Sign out', destroy_user_session_path, :method=>'delete' %></li>
<% end %>

the root error of your problem is that you haven't use RESTful routes in your "link_to". 问题的根本原因是您没有在“ link_to”中使用RESTful路由。

you should correct your code to: 您应该将代码更正为:

<%= link_to "Sign Out", destroy_user_session_path, :method => :delete %>

so that it will match the routes 这样就可以匹配路线

DELETE /users(.:format)  {:action=>"destroy", :controller=>"devise/registrations" }

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

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