在RESTFUL身份验证中添加注销按钮 [英] Add a logout button in RESTFUL authentication
问题描述
我已经安装了RESTFUL身份验证,并且一切似乎都工作正常.我可以注册并登录.我可以注销的唯一方法是输入URL http://localhost:3000/logout
如何在页面上添加注销按钮?我尝试将一个添加到members.rhtml
<%= link_to "logout", :controller=> "sessions", :action=> "destroy" %>
引用了session_controller.rb 但我收到一个错误没有动作响应显示.动作:创建,销毁和新建"
有什么想法吗?谢谢您的路由文件中包含什么?
尝试放置
map.log_out 'logout', :controller => 'sessions', :action => 'destroy'
在您的路线上.
然后只有
<%= link_to "Sign out", log_out_url %>
退出链接.
编辑
这一切都取决于您如何指定路由.
因为您在路由中包含map.log_out,所以该URL http://localhost:3000/logout 网址就会被此网址提取并路由到正确的操作.
如果您有:
<%= link_to "logout", :controller=> "sessions", :action=> "destroy" %>
这只会为您生成一个 http://localhost:3000/session 的链接.但是,它对路由没有任何作用.您仍然需要指定正确的路线.
请注意,Rails不会将destroy操作附加到URL. (不会创建 http://localhost:3000/session/destroy .) em>假定如果您执行销毁操作,则将使用DELETE http动词发送它.由于某种原因,它不是很完美,实际上也没有默认发送DELETE动词.
您可以强制其执行此操作:
<%= link_to "logout", {:controller=> "user_sessions", :action=> "destroy"}, :method => :delete%>
除非您也正确地路由它,否则它仍然不起作用.如果您将以下内容放入路线:
map.resource :session
然后,rails将为所有动词生成路由,并为其指定默认动作,包括DELETE.可在此处找到更多信息:从外部进入内部的路线
整个页面值得一遍又一遍地阅读,直到您真正理解为止.路由是了解Rails的关键!
对于像Sessions这样的简单控制器,只需要指定log_out路由然后链接到log_out_url即可.
(希望如此,睡眠不足正在蔓延!)
I've installed RESTFUL authentication and everything seems to be working fine. i can signup and login. the only way i can logout is by typing in the URL http://localhost:3000/logout
how do i add a logout button on a page? i tried adding one to the members.rhtml
<%= link_to "logout", :controller=> "sessions", :action=> "destroy" %>
which references the session_controller.rb but i get an error "No action responded to show. Actions: create, destroy, and new"
any thoughts? thanx
What do you have in your routes file?
Try putting
map.log_out 'logout', :controller => 'sessions', :action => 'destroy'
in your routes.
Then just have
<%= link_to "Sign out", log_out_url %>
for the sign out link.
EDIT
Its all down to how you specify the routing.
Because you had the map.log_out in the routing, then the url http://localhost:3000/logout url is picked up by this and routed to the correct action.
If you have :
<%= link_to "logout", :controller=> "sessions", :action=> "destroy" %>
This will just generate a link for you of http://localhost:3000/session. But, it does nothing to the routing. You still need to specify the correct routes.
Note that Rails does not append the destroy action to the url. (It will not create http://localhost:3000/session/destroy.) It assumes that if you have an action of destroy that you will be sending it with a DELETE http verb. For some reason, its not quite perfect and it doesnt actually also default to sending the DELETE verb.
You can force it to do this :
<%= link_to "logout", {:controller=> "user_sessions", :action=> "destroy"}, :method => :delete%>
This will still not work unless you also route it correctly. If you put the following into the routes :
map.resource :session
Then rails will generate the routing for all the verbs and specify the default actions for them, including DELETE. More information can be found here : Rails Routing from the Outside In.
That whole page is worth reading over and over until you really understand it. Routing is key to understanding Rails!
For a simple controller like Sessions, it is easier just to specify the log_out route and then link to log_out_url..
(Hope that makes sense, sleep deprivation is creeping in!)
这篇关于在RESTFUL身份验证中添加注销按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!