Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 768
holyredbeard Avatar asked Feb 24 '26 17:02

holyredbeard


2 Answers

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| %>
like image 122
jvnill Avatar answered Feb 26 '26 05:02

jvnill


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| %>
like image 31
Manoj Monga Avatar answered Feb 26 '26 07:02

Manoj Monga



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!