No route matches [POST] “/sessions/user”

2020-06-16 05:24发布

问题:

I just installed Devise on my app, I had previously done it by scratch like Michael Hartl tutorial.

Currently I can sign up and log out. But when I do log in it gives an error:

No route matches [POST] "/sessions/user"

it happens when I click on the sign in/log in button with or without the (correct) password.

My route file is:

SampleApp::Application.routes.draw do
devise_for :users, path_names: { sign_in: "login", sign_out: "logout"}

resources :users do
  resources :bookings, only: [:show]
  end
resources :bookings
resources :sessions

# match '/signup',  to: 'devise/registrations#new', via: :get
# match '/signin',  to: 'devise/sessions#new', via: [:post, :get]
# match '/signout', to: 'devise/sessions#destroy', via: :delete
match '/admin',   to: 'admin#new', via:  :get
match "bookings/new", to: 'bookings#new', via: [:post, :get]

devise_scope :user do 
  root to: 'static_pages#home'
end

[EDIT]

My form is the default from devise:

<h2>Sign in</h2>

<%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
  <div><%= f.label :email %><br />
  <%= f.email_field :email, :autofocus => true %></div>
  <div><%= f.label :password %><br />
  <%= f.password_field :password %></div>
  <% if devise_mapping.rememberable? -%>
    <div><%= f.check_box :remember_me %>
    <%= f.label :remember_me %></div>
  <% end -%>
  <div><%= f.submit "Sign in" %></div>
<% end %>

Currently I don't have any code on Session Controller nor Helper like in the Michael Hartl tutorial

My rake routes regarding Sessions are as follows:

            sessions GET      /sessions(.:format)                    sessions#index
                     POST     /sessions(.:format)                    sessions#create
         new_session GET      /sessions/new(.:format)                sessions#new
        edit_session GET      /sessions/:id/edit(.:format)           sessions#edit
             session GET      /sessions/:id(.:format)                sessions#show
                     PATCH    /sessions/:id(.:format)                sessions#update
                     PUT      /sessions/:id(.:format)                sessions#update
                     DELETE   /sessions/:id(.:format)                sessions#destroy

[SOLVED]

I updated the routes file

  devise_scope :user do 
    root to: 'static_pages#home'
    match '/sessions/user', to: 'devise/sessions#create', via: :post
  end

回答1:

Add the following lines in the routes file

post ':controller(/:action(/:id(.:format)))'
get ':controller(/:action(/:id(.:format)))'

Also remove match ':controller(/:action(/:id(.:format)))' ->causes a conflict in later versions



回答2:

A possible solution I came up is:

 devise_scope :user do 
    root to: 'static_pages#home'
    match '/sessions/user', to: 'devise/sessions#create', via: :post
  end


回答3:

In the sessions/new view, check the link you've included in the sign-in button. It's possible you have user instead of :user or a similar typo that goes to the wrong path. Also check the output of $ rake routes - is the sign-in button pointing to a path that exists? Have you made some named routes in routes.rb?

In the Hartl tutorial, if you've followed the default, when you log in it should be directing you from

sessions#new to users#show (the user_path)