No route matches {:action=>"show", :controller=>"users"}

15,057

Solution 1

your error is in this line

<%= form_for("user", :url => user_path) do |f| %>

user_path is expecting an id. if you change that to users_path, that should fix it but I don't think that's your intention.

UPDATE: to use the login action on the users controller, you need to update your routes

resources :users do
  post :login, on: :collection, as: :login
end

passing the :as option creates a named_route for you called login_users_path which you can use on your form_for. and since we wanted to do a post, we also need to specify that in the form_for

<%= form_for("user", :url => login_users_path, :html => { :method => :post }) do |f| %>

Solution 2

Update your routes.rb to look like:

get "home/index"

resources :users do
  post :login, :on => :collection
end

resources :projects
resources :tickets

root :to => 'home#index'

and in your view file change the form_for line to be:

<%= form_for("user", :url => login_users_path) do |f| %>

Solution 3

resources :users only adds default routes. If you want to add new action (other then defaults) you need to use 'collection. And you can specify the method get or post. After adding to routes.rb. You can get the path by running rake routes then you add the correct route in the action of form.

   resources :users, :collection => {:login => :post}
Share:
15,057
holyredbeard
Author by

holyredbeard

Updated on June 04, 2022

Comments

  • holyredbeard
    holyredbeard almost 2 years

    I'm trying to implement a Twitter Boostrap login form, that's gonna be used on every page (because the navigation bar is a part of the layout).

    However, when trying the code below I get the following error:

    No route matches {:action=>"show", :controller=>"users"}
    

    User controller:

    class UsersController < ApplicationController
    
      def index
        @users = User.all
      end
    
      def show
        ...
      end
    
      def login
        ...
      end
    
    end
    

    _navigation.html.erb:

    <div class="dropdown-menu" style="padding: 15px; padding-bottom: 0px;">
        <%= form_for("user", :url => user_path) do |f| %>
            <%= f.label :email%>
            <%= f.text_field(:email, :size => 30, :class => 'login_field', :placeholder => 'Användarnamn')%>
            <%= f.label :password%>
            <%= f.text_field(:password, :size => 30, :class => 'login_field', :placeholder => 'Lösenord')%>
    
            <%= f.submit "Logga in", :class => 'login_submit btn btn-primary' %>
        <% end %>
    </div>
    

    config/routes.rb:

    get "home/index"
    
    resources :users
    resources :projects
    resources :tickets
    
    root :to => 'home#index'
    

    rake routes (that has to do with users):

        users GET    /users(.:format)             users#index
              POST   /users(.:format)             users#create
     new_user GET    /users/new(.:format)         users#new
    edit_user GET    /users/:id/edit(.:format)    users#edit
         user GET    /users/:id(.:format)         users#show
              PUT    /users/:id(.:format)         users#update
              DELETE /users/:id(.:format)         users#destroy
    

    I'm new to Rails but find it strange that it complains that the route doesn't exist because the action "show" is to be found inside the user controller.

    The other thing I'm wondering about is why it looks for the action "show", while it should be "login" in this case?

    Why is this happening and what shall I do?

  • holyredbeard
    holyredbeard over 11 years
    Thanks for the answer! I tried the code but get the following error: "undefined local variable or method `login_path' for #<#<Class:0x007f8556a37c98>:0x007f8556f3a1c8>"
  • jvnill
    jvnill over 11 years
    sorry that should be login_users_path, forgot that it's under a resource