Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Devise routing error - "No route matches" (controller=>"devise/sessions")

Edit 2: Looks like a quick temp fix for me was to add a forward slash "/" in front of the controller name in my link_to_unless_current and current_page methods from my view. e.g.

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

Here is more info on the problem I encountered in case anyone else runs into the same thing. https://github.com/plataformatec/devise/issues/471

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

My devise path broke when I made my navigation (_header.html.erb) dynamic. I'm hoping this is a simple fix, but really lost atm. Every page on my site works, except when I go to /admin and then I get the routing error below:

Here's the exact error I receive:

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'

Here is the config/routes.rb file:

  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'

Here's the app/views/layouts/_header.html.erb file:

<% 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 -%>

And here's the output of 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"}

Thanks for looking!

Edit 1: When I tried the suggestion from @Dogbert, it didn't seem to make a difference unfortunately:

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>
like image 487
coasthird Avatar asked Mar 13 '11 22:03

coasthird


1 Answers

Try doing

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

The problem seems to be that, when you don't specify the controller in a link_to, it automatically uses the current page's controller. In this case, it tried using devise/sessions controller, with coupons action.

like image 98
Dogbert Avatar answered Nov 19 '22 18:11

Dogbert