没有路由匹配[POST]“/ sessions / user”

时间:2013-08-22 22:49:39

标签: ruby-on-rails devise ruby-on-rails-4

我刚刚在我的应用程序上安装了Devise,我之前已经像Michael Hartl教程一样完成了它。

目前我可以注册并退出。但是当我登录时会出错:

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

当我点击登录/登录按钮时,无论是否有(正确的)密码,都会发生这种情况。

我的路线档案是:

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

[编辑]

我的表单是设计的默认设置:

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

目前我在Session Controller上没有任何代码,也没有像Michael Hartl教程那样的Helper代码

关于Sessions的我的佣金路线如下:

            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

[解决]

我更新了路线文件

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

3 个答案:

答案 0 :(得分:9)

在路线文件中添加以下行

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

同时删除 match ':controller(/:action(/:id(.:format)))' - &gt;会在以后的版本中导致冲突

答案 1 :(得分:5)

我提出的一个可能的解决方案是:

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

答案 2 :(得分:0)

sessions/new视图中,检查您在登录按钮中包含的链接。你可能有user而不是:user或类似错误的错误路径。还要检查$ rake routes的输出 - 是否是指向存在的路径的登录按钮?您是否在routes.rb

中创建了一些命名路线

在Hartl教程中,如果您遵循默认设置,当您登录时应该指示您

sessions#newusers#showuser_path