Rails 3 haml 视图中带有设计的命名路由

发布于 2024-09-24 14:07:42 字数 5036 浏览 5 评论 0原文

从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

木落 2024-10-01 14:07:42

关于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.

牛↙奶布丁 2024-10-01 14:07:42

user_url 位于末尾

user _url at the end

£烟消云散 2024-10-01 14:07:42

destroy_admin_session 应写为 destroy_admin_session_path

destroy_admin_session should be written as destroy_admin_session_path

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文