Why does devise put /devise/ in front of every lin

2019-06-01 15:56发布

问题:

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.

回答1:

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.



回答2:

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?



回答3:

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"}