Rails设计路由错误 - “没有路由匹配”(controller =>“devise / sessions”)

时间:2011-03-13 22:05:48

标签: ruby-on-rails ruby routing

编辑2:对我来说,快速修复一下就是在我的视图中使用link_to_unless_current和current_page方法在控制器名称前添加正斜杠“/”。 例如

<% active = current_page?(:controller => '/sites', :action => action) ? 'active' : '' %>
      <li class="<%= active %>"><%= link_to_unless_current(anchor, { :controller => '/sites', :action => action }) %></li>

以下是我遇到的问题的更多信息,以防其他人遇到同样的事情。 https://github.com/plataformatec/devise/issues/471

=============================================== =============

当我的导航(_header.html.erb)动态时,我的设计路径中断了。我希望这是一个简单的修复,但真的失去了atm。我的网站上的每个页面都有效,除非我去/ admin,然后我收到下面的路由错误:

以下是我收到的确切错误:

Started GET "/admin" for 75.13.95.55 at 2011-03-13 15:40:49 -0500
  Processing by AdminController#index as HTML
Completed   in 2ms


Started GET "/users/sign_in" for 75.13.95.55 at 2011-03-13 15:40:51 -0500
  Processing by Devise::SessionsController#new as HTML
Rendered /usr/local/rvm/gems/ruby-1.9.2-p0/gems/devise-1.1.5/app/views/devise/shared/_links.erb (1.9ms)
Rendered layouts/_header.html.erb (37.0ms)
Rendered /usr/local/rvm/gems/ruby-1.9.2-p0/gems/devise-1.1.5/app/views/devise/sessions/new.html.erb within layouts/application (57.5ms)
Completed   in 91ms

ActionView::Template::Error (No route matches {:action=>"coupons", :controller=>"devise/sessions"}):
    7: <div id="navcontainer">
    8:   <ul id="navlist">
    9:     <% nav.each do |anchor, action| %>
    10:       <% active = current_page?(:action => action) ? 'active' : '' %>
    11:       <li class="<%= active %>"><%= link_to_unless_current(anchor, { :action => action }) %></li>
    12:     <% end -%>
    13:   </ul>
  app/views/layouts/_header.html.erb:10:in `block in _app_views_layouts__header_html_erb__482084855_91994390_872614734'
  app/views/layouts/_header.html.erb:9:in `each'
  app/views/layouts/_header.html.erb:9:in `_app_views_layouts__header_html_erb__482084855_91994390_872614734'
  app/views/layouts/application.html.erb:17:in `_app_views_layouts_application_html_erb___293959382_92160660__194406143'

以下是config/routes.rb文件:

  devise_for :users

  match '/info' => 'sites#info', :as => :info
  match "/reviews" => 'sites#reviews', :as => :reviews

  resources :admin do
    collection do
      put 'moderate'
      get 'approved'
    end
  end

  root :to => 'sites#coupons'

这是app/views/layouts/_header.html.erb文件:

<% nav = { 'Coupons' => 'coupons', 'Reviews' => 'reviews', 'Info' => 'info' } %>

<div id="navcontainer">
  <ul id="navlist">
    <% nav.each do |anchor, action| %>
      <% active = current_page?(:action => action) ? 'active' : '' %>
      <li class="<%= active %>"><%= link_to_unless_current(anchor, { :action => action }) %></li>
    <% end -%>

这是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"}
                  info        /info(.:format)                    {:controller=>"sites", :action=>"info"}
               reviews        /reviews(.:format)                 {:controller=>"sites", :action=>"reviews"}
moderate_admin_index PUT    /admin/moderate(.:format)        {:action=>"moderate", :controller=>"admin"}
approved_admin_index GET    /admin/approved(.:format)        {:action=>"approved", :controller=>"admin"}
         admin_index GET    /admin(.:format)                 {:action=>"index", :controller=>"admin"}
                       POST   /admin(.:format)                 {:action=>"create", :controller=>"admin"}
           new_admin GET    /admin/new(.:format)             {:action=>"new", :controller=>"admin"}
          edit_admin GET    /admin/:id/edit(.:format)        {:action=>"edit", :controller=>"admin"}
               admin GET    /admin/:id(.:format)             {:action=>"show", :controller=>"admin"}
                       PUT    /admin/:id(.:format)             {:action=>"update", :controller=>"admin"}
                       DELETE /admin/:id(.:format)             {:action=>"destroy", :controller=>"admin"}
                  root        /(.:format)                        {:controller=>"sites", :action=>"coupons"}

感谢您的期待!

编辑1: 当我尝试@Dogbert的建议时,不幸的是它似乎没有什么区别:

No route matches {:controller=>"devise/sites", :action=>"coupons"}
Extracted source (around line #9):

6: <div id="navcontainer">
7:   <ul id="navlist">
8:     <% nav.each do |anchor, action| %>
9:       <% active = current_page?(:controller => 'sites', :action => action) ? 'active' : '' %>
10:       <li class="<%= active %>"><%= link_to_unless_current(anchor, { :controller => 'sites', :action => action }) %></li>
11:     <% end -%>
12:   </ul>

5 个答案:

答案 0 :(得分:2)

尝试

  <li class="<%= active %>"><%= link_to_unless_current(anchor, { :controller => 'sites', :action => action }) %></li>

问题似乎是,当你没有在link_to中指定controller时,它会自动使用当前页面的控制器。在这种情况下,它尝试使用devise/sessions控制器,coupons操作。

答案 1 :(得分:0)

我会调查这样的命名空间:

命名空间:admin do     root:to =&gt; “碱#指数”     资源:用户做的       资源:权限     结束   端

答案 2 :(得分:0)

我建议使用路由助手来代替哈希。为站点部分创建路径,如下所示:

scope :controller => "sites" do
  get 'somewhere', :as => "somewhere"
  # etc...
end

然后在您的视图中将其引用为somewhere_urlsomewhere_path

答案 3 :(得分:0)

检查控制器并查看路径

每个人都必须来user_sessionsuser_session

见:

形式:

<%= form_for @user_sessions, :url => {:action => "create"} do |f| %>

路线:

get "user_session/new"
    match 'login' => "user_sessions#new",      :as => :login 
    match 'logout' => "user_sessions#destroy", :as => :logout
resources :user_sessions

Controller还提到user_sessions

Rails设计路由错误 - “没有路由匹配”(controller =&gt;“devise / sessions”)

这种类型的错误将会出现

答案 4 :(得分:0)

从应用程序控制器内的rescue_from块调用帮助程序时出现了同样的错误。我所做的是使用像这个

这样的完全限定路径作为助手
Rails.application.routes.url_helpers.new_user_session_path

希望它有助于未来的googlers。