Chris
Chris

Reputation: 123

Ruby on rails "No route matches"

I am new to Rails and am just implementing some basic applications. Just starting on my second app and have run into what is a basic problem, but Google is yielding me nothing.

Getting this error:

No route matches {:controller=>"user", :action=>"admin_login"}

Here is my routes.rb

Blah::Application.routes.draw do
  resources :items, :cart, :user
end

Here is my applications.html.erb (the idea is this is a header of course, and I'm trying to create a link to 'login'. Right now it's just supposed to set the 'login' session variable to '1'.

<!DOCTYPE html>
<html>
<head>
  <title>Blah</title>
  <%= stylesheet_link_tag :all %>
  <%= javascript_include_tag :defaults %>
  <%= csrf_meta_tag %>
</head>
<body>

<div id="headerHolder">
    <div id="title">blah</div>
    <div id="menu">
        <div class ="menuItem">blog</div>
        <div class ="menuItem">
            <%= link_to "products",  :controller => "items", 
                                     :action => "index" %>
        </div>
        <div class ="menuItem">contact</div>    
        <div class="menuItem">
            <%= link_to "cart",  :controller => "cart", 
                                 :action => "index" %>
        </div>
        <div class="menuItem">
                <%= link_to_unless_current "admin", :controller => "user", 
                                                    :action => "admin_login" %>
        </div>
    </div>
</div>

<div id="content">
    <%= yield %>
</div>

</body>
</html>

And this is my user_controller.rb

class UserController < ApplicationController

  def index
  end

  def admin_login
    session[:login] = 1
    session[:cart] = nil
    flash[:notice] = "Admin user successfully logged in, cart reset."
    redirect_to :controller => :items
  end

end

What am i missing in my routes.rb? Or otherwise...am sure it's something daft.

Upvotes: 12

Views: 72562

Answers (5)

Deepak Kalhan
Deepak Kalhan

Reputation: 1

There is a new method in Rails 3. You can use the following:

get 'admin_login' => "user#admin_login"

Upvotes: 0

Anubhaw
Anubhaw

Reputation: 6068

You need to add admin_login method to routes, like:-

map.connect '/user/admin_login', :controller => 'users', :action => 'admin_login'

Upvotes: 7

Simon Perepelitsa
Simon Perepelitsa

Reputation: 20639

For Rails > 3 you should use the new routing syntax:

resources :items, :cart

resource :user do
  # Route GET /user/admin_login
  get 'admin_login', :on => :collection
end

See Rails guides for more information about routing.

Upvotes: 21

user1875926
user1875926

Reputation: 131

you can use

match 'admin_login' => 'user#admin_login', :as =>'admin_login'

default method for this call is post u can change method behavior by using

:via => [:post/:put/:get]

Upvotes: 1

stonenotes
stonenotes

Reputation: 19

find “config/routes.rb” file, edit, Locate the following line:

# See how all your routes lay out with "rake routes"

In this line add the following line, as follows:

map.connect '',:controller=>"index",:action=>"index"

Upvotes: 1

Related Questions