为什么设计put / devise /在每个link_to锚点前?

时间:2011-05-02 00:24:13

标签: ruby-on-rails devise jruby

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

  1. 为什么这样做?看起来像奇怪的默认行为
  2. 如何阻止它添加/设计到每个link_to()?
  3. 我的路线档案:

    devise_for :users
    
    get "/webpages/:page" => "webpages#show", :as => :show_webpage
    
    root :to => "webpages#index"
    

    我的'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"}
    

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

    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 /”。这根本不是我想要的。

3 个答案:

答案 0 :(得分:2)

我终于明白了。

      <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”命名空间或父目录下。

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

答案 1 :(得分:1)

在较新版本的Rails中,您可以这样做:

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

您只需将_path附加到您执行'rake路线'时看到的指定路线上。将_url追加到命名路由将为您提供URL字符串BTW。这可能很有用。

伊恩。

答案 2 :(得分:0)

所有旨在设计的链接/表单都应以“/ 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"}