Rails 3 haml 视图中带有设计的命名路由
从 rake 路线 我在我的应用程序中得到了这个
new_admin_session GET /admins/sign_in(.:format) {:controller=>"devise/sessions", :action=>"new"}
admin_session POST /admins/sign_in(.:format) {:controller=>"devise/sessions", :action=>"create"}
destroy_admin_session GET /admins/sign_out(.:format) {:controller=>"devise/sessions", :action=>"destroy"}
admin_password POST /admins/password(.:format) {:controller=>"devise/passwords", :action=>"create"}
new_admin_password GET /admins/password/new(.:format) {:controller=>"devise/passwords", :action=>"new"}
edit_admin_password GET /admins/password/edit(.:format) {:controller=>"devise/passwords", :action=>"edit"}
admin_password PUT /admins/password(.:format) {:controller=>"devise/passwords", :action=>"update"}
admin_registration POST /admins(.:format) {:controller=>"devise/registrations", :action=>"create"}
new_admin_registration GET /admins/sign_up(.:format) {:controller=>"devise/registrations", :action=>"new"}
edit_admin_registration GET /admins/edit(.:format) {:controller=>"devise/registrations", :action=>"edit"}
admin_registration PUT /admins(.:format) {:controller=>"devise/registrations", :action=>"update"}
admin_registration DELETE /admins(.:format) {:controller=>"devise/registrations", :action=>"destroy"}
new_user_session GET /users/sign_in(.:format) {:controller=>"devise/sessions", :action=>"new"}
user_session POST /users/sign_in(.:format) {:controller=>"devise/sessions", :action=>"create"}
destroy_user_session GET /users/sign_out(.:format) {:controller=>"devise/sessions", :action=>"destroy"}
user_password POST /users/password(.:format) {:controller=>"devise/passwords", :action=>"create"}
new_user_password GET /users/password/new(.:format) {:controller=>"devise/passwords", :action=>"new"}
edit_user_password GET /users/password/edit(.:format) {:controller=>"devise/passwords", :action=>"edit"}
user_password PUT /users/password(.:format) {:controller=>"devise/passwords", :action=>"update"}
user_registration POST /users(.:format) {:controller=>"devise/registrations", :action=>"create"}
new_user_registration GET /users/sign_up(.:format) {:controller=>"devise/registrations", :action=>"new"}
edit_user_registration GET /users/edit(.:format) {:controller=>"devise/registrations", :action=>"edit"}
user_registration PUT /users(.:format) {:controller=>"devise/registrations", :action=>"update"}
user_registration DELETE /users(.:format) {:controller=>"devise/registrations", :action=>"destroy"}
root /(.:format) {:controller=>"devise/sessions", :action=>"new"}
change_authorization_rules GET /authorization_rules/change(.:format) {:controller=>"authorization_rules", :action=>"change"}
suggest_change_authorization_rules GET /authorization_rules/suggest_change(.:format) {:controller=>"authorization_rules", :action=>"suggest_change"}
graph_authorization_rules GET /authorization_rules/graph(.:format) {:controller=>"authorization_rules", :action=>"graph"}
authorization_rules GET /authorization_rules(.:format) {:controller=>"authorization_rules", :action=>"index"}
authorization_usages GET /authorization_usages(.:format) {:controller=>"authorization_usages", :action=>"index"}
在 haml 视图中我这样做了
%html
%head
%title="Devise Demo"
= stylesheet_link_tag :all
= javascript_include_tag :defaults
= csrf_meta_tag
%body
%p.notice=notice
%p.alert=alert
- if current_user
= "Hello #{current_user.email} ("
= link_to 'Not you?', destroy_admin_session
= ")"
,但我得到了错误
undefined local variable or method `destroy_admin_session' for #<#<Class:0x7f66ce2d3610>:0x7f66ce2cd1c0>
我显然错过了 Rails 3
Brad中关于命名路线的明显信息
from rake routes I get this in my application
new_admin_session GET /admins/sign_in(.:format) {:controller=>"devise/sessions", :action=>"new"}
admin_session POST /admins/sign_in(.:format) {:controller=>"devise/sessions", :action=>"create"}
destroy_admin_session GET /admins/sign_out(.:format) {:controller=>"devise/sessions", :action=>"destroy"}
admin_password POST /admins/password(.:format) {:controller=>"devise/passwords", :action=>"create"}
new_admin_password GET /admins/password/new(.:format) {:controller=>"devise/passwords", :action=>"new"}
edit_admin_password GET /admins/password/edit(.:format) {:controller=>"devise/passwords", :action=>"edit"}
admin_password PUT /admins/password(.:format) {:controller=>"devise/passwords", :action=>"update"}
admin_registration POST /admins(.:format) {:controller=>"devise/registrations", :action=>"create"}
new_admin_registration GET /admins/sign_up(.:format) {:controller=>"devise/registrations", :action=>"new"}
edit_admin_registration GET /admins/edit(.:format) {:controller=>"devise/registrations", :action=>"edit"}
admin_registration PUT /admins(.:format) {:controller=>"devise/registrations", :action=>"update"}
admin_registration DELETE /admins(.:format) {:controller=>"devise/registrations", :action=>"destroy"}
new_user_session GET /users/sign_in(.:format) {:controller=>"devise/sessions", :action=>"new"}
user_session POST /users/sign_in(.:format) {:controller=>"devise/sessions", :action=>"create"}
destroy_user_session GET /users/sign_out(.:format) {:controller=>"devise/sessions", :action=>"destroy"}
user_password POST /users/password(.:format) {:controller=>"devise/passwords", :action=>"create"}
new_user_password GET /users/password/new(.:format) {:controller=>"devise/passwords", :action=>"new"}
edit_user_password GET /users/password/edit(.:format) {:controller=>"devise/passwords", :action=>"edit"}
user_password PUT /users/password(.:format) {:controller=>"devise/passwords", :action=>"update"}
user_registration POST /users(.:format) {:controller=>"devise/registrations", :action=>"create"}
new_user_registration GET /users/sign_up(.:format) {:controller=>"devise/registrations", :action=>"new"}
edit_user_registration GET /users/edit(.:format) {:controller=>"devise/registrations", :action=>"edit"}
user_registration PUT /users(.:format) {:controller=>"devise/registrations", :action=>"update"}
user_registration DELETE /users(.:format) {:controller=>"devise/registrations", :action=>"destroy"}
root /(.:format) {:controller=>"devise/sessions", :action=>"new"}
change_authorization_rules GET /authorization_rules/change(.:format) {:controller=>"authorization_rules", :action=>"change"}
suggest_change_authorization_rules GET /authorization_rules/suggest_change(.:format) {:controller=>"authorization_rules", :action=>"suggest_change"}
graph_authorization_rules GET /authorization_rules/graph(.:format) {:controller=>"authorization_rules", :action=>"graph"}
authorization_rules GET /authorization_rules(.:format) {:controller=>"authorization_rules", :action=>"index"}
authorization_usages GET /authorization_usages(.:format) {:controller=>"authorization_usages", :action=>"index"}
In a haml view I do
%html
%head
%title="Devise Demo"
= stylesheet_link_tag :all
= javascript_include_tag :defaults
= csrf_meta_tag
%body
%p.notice=notice
%p.alert=alert
- if current_user
= "Hello #{current_user.email} ("
= link_to 'Not you?', destroy_admin_session
= ")"
but I get the error
undefined local variable or method `destroy_admin_session' for #<#<Class:0x7f66ce2d3610>:0x7f66ce2cd1c0>
I'm obviously missing the abvious about named routes in Rails 3
Brad
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
关于
rake paths
的技巧是,第一列中显示的路径不是助手的名称,而是前缀,因为有_url 和
_path
变体。使用
destroy_admin_session_path
。The trick about
rake routes
is that the path shown in the first column isn't the name of the helper, it's the prefix, because there are_url
and_path
variants.Use
destroy_admin_session_path
.user_url 位于末尾
user _url at the end
destroy_admin_session
应写为destroy_admin_session_path
destroy_admin_session
should be written asdestroy_admin_session_path