为什么 devise 将 /devise/ 放在每个 link_to 锚点前面?

发布于 2024-11-04 15:01:42 字数 2936 浏览 3 评论 0原文

我刚刚生成了设计的所有视图,并且开始自定义登录屏幕。除了登录页面上生成的所有以“/devise”开头的链接之外,它的效果很好。

  1. 它为什么要这么做?看起来很奇怪的默认行为
  2. 如何阻止它向每个 link_to() 添加 /devise?

我的路线文件:

devise_for :users

get "/webpages/:page" => "webpages#show", :as => :show_webpage

root :to => "webpages#index"

我的“rake 路线”

        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 GET    /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"}
            show_webpage GET    /webpages/:page(.:format)      {:controller=>"webpages", :action=>"show"}
                    root        /(.:format)                    {:controller=>"webpages", :action=>"index"}

当我尝试渲染来自设计控制器的页面时收到的错误消息:

ActionController::RoutingError in Devise/sessions#new

Showing /home/egervari/Projects/training/app/views/layouts/application.html.erb where line #21 raised:

No route matches {:controller=>"devise/webpages", :action=>"show", :page=>"tour"}

Extracted source (around line #21):

18:           </a>
19:         </li>
20:         <li>
21:           <%= link_to("Tour", :controller => "webpages", :action => "show", :page => "tour") %>
22:         </li>
23:         <li>
24:           <%= link_to("Why Use Us?", :controller => "webpages", :action => "show", :page => "why") %>

如您在上面看到的,它正在尝试将“devise/”添加到我的链接。这根本不是我想要的。

I just generated all the views for devise, and I'm starting to customize the login screen. It works great except for all the links that are generated on the sign in page start with "/devise".

  1. Why is it doing that? Seems like odd default behaviour
  2. How do I stop it from adding /devise to every link_to()?

My routes file:

devise_for :users

get "/webpages/:page" => "webpages#show", :as => :show_webpage

root :to => "webpages#index"

My 'rake routes'

        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 GET    /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"}
            show_webpage GET    /webpages/:page(.:format)      {:controller=>"webpages", :action=>"show"}
                    root        /(.:format)                    {:controller=>"webpages", :action=>"index"}

The error message I get when I try and render a page that comes from the devise controller:

ActionController::RoutingError in Devise/sessions#new

Showing /home/egervari/Projects/training/app/views/layouts/application.html.erb where line #21 raised:

No route matches {:controller=>"devise/webpages", :action=>"show", :page=>"tour"}

Extracted source (around line #21):

18:           </a>
19:         </li>
20:         <li>
21:           <%= link_to("Tour", :controller => "webpages", :action => "show", :page => "tour") %>
22:         </li>
23:         <li>
24:           <%= link_to("Why Use Us?", :controller => "webpages", :action => "show", :page => "why") %>

As you can see above, it's trying to add "devise/" to my link. This is not what I want at all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

沧笙踏歌 2024-11-11 15:01:50

我终于明白了。

      <li><%= link_to("Terms and Use", :controller => "/webpages", :action => "show", :page => "terms") %> |</li>
      <li><%= link_to("Privacy Policy", :controller => "/webpages", :action => "show", :page => "privacy") %> |</li>

基本上我所做的就是放置“/webpages”而不是“webpages”来告诉rails这些控制器不在“devise”命名空间或父目录下。

这是适当的修复吗?有更简单的解决方案吗?

I figured it out finally.

      <li><%= link_to("Terms and Use", :controller => "/webpages", :action => "show", :page => "terms") %> |</li>
      <li><%= link_to("Privacy Policy", :controller => "/webpages", :action => "show", :page => "privacy") %> |</li>

Basically what I did was put "/webpages" instead of "webpages" to tell rails that these controllers were not under the "devise" namespace or parent directory.

Is this the appropriate fix? Is there a simpler solution?

森林很绿却致人迷途 2024-11-11 15:01:50

在较新版本的 Rails 中,您可以执行以下操作:

<%= link_to "Privacy Policy", show_webpage_path(:page => 'privacy') %>

只需将 _path 附加到执行“rake 路线”时看到的命名路线即可。顺便说一句,将 _url 附加到命名路由将为您提供 URL 字符串。这可能很有用。

伊恩。

In newer versions of Rails you can do:

<%= link_to "Privacy Policy", show_webpage_path(:page => 'privacy') %>

You just append a _path onto the named route that you see when you do a 'rake routes'. Appending _url to the named route will give you the URL string, BTW. Which can be useful.

ian.

如歌彻婉言 2024-11-11 15:01:50

所有针对 devise 的链接/表单均应以“/devise”开头。请参阅下面由 devise 生成的路由。为什么你想改变这种行为?是不是不起作用?或者您需要/想要定制设备控制器吗?

          new_user_session GET    /users/login(.:format)                                           {:action=>"new", :controller=>"devise/sessions"}
              user_session POST   /users/login(.:format)                                           {:action=>"create", :controller=>"devise/sessions"}
      destroy_user_session GET    /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/register(.: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"}
         user_confirmation POST   /users/confirmation(.:format)                                    {:action=>"create", :controller=>"devise/confirmations"}
     new_user_confirmation GET    /users/confirmation/new(.:format)                                {:action=>"new", :controller=>"devise/confirmations"}
                           GET    /users/confirmation(.:format)                                    {:action=>"show", :controller=>"devise/confirmations"}

All links/forms targeted to devise are expected to start with "/devise". See the routes generated by devise below. Why do you want change this behavior? Is it not working? Or do you need/want to customize the devise controllers?

          new_user_session GET    /users/login(.:format)                                           {:action=>"new", :controller=>"devise/sessions"}
              user_session POST   /users/login(.:format)                                           {:action=>"create", :controller=>"devise/sessions"}
      destroy_user_session GET    /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/register(.: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"}
         user_confirmation POST   /users/confirmation(.:format)                                    {:action=>"create", :controller=>"devise/confirmations"}
     new_user_confirmation GET    /users/confirmation/new(.:format)                                {:action=>"new", :controller=>"devise/confirmations"}
                           GET    /users/confirmation(.:format)                                    {:action=>"show", :controller=>"devise/confirmations"}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文