简体   繁体   English

Rails 3 RoutingError:没有路由匹配{:controller =>“user_sessions”}

[英]Rails 3 RoutingError: No route matches {:controller=>“user_sessions”}

I'll just try to outline this as high level as I can. 我会尽量把它概括为高水平。 I am trying to access http://localhost:3000/login 我正在尝试访问http:// localhost:3000 / login

The error is: 错误是:

No route matches {:controller=>"user_sessions"}

And it's erroring on this line in the new.html.erb file below: 并且它在下面的new.html.erb文件中的这行上有错误:

<%= form_for(@user_session) do |f| %>

The route in routes.rb is: routes.rb中的路由是:

match 'login' => 'user_sessions#new', :as => :login

The user_sessions_controller.rb is: user_sessions_controller.rb是:

class UserSessionsController < ApplicationController
  def new
    @user_session = UserSession.new
  end

  def create
    @user_session = UserSession.new(params[:user_session])
    if @user_session.save
      flash[:notice] = "Successfully logged in."
      redirect_to root_path
    else
      render :action => 'new'
    end
  end

  def destroy
    @user_session = UserSession.find
    @user_session.destroy
    flash[:notice] = "Successfully logged out."
    redirect_to root_path
  end
end

And the actual view for the login page is as follows (new.html.erb): 登录页面的实际视图如下(new.html.erb):

<h1>Login</h1>

<%= form_for(@user_session) do |f| %>
  <% if @user_session.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@user_session.errors.count, "error") %> prohibited this user from being saved:</h2>

      <ul>
      <% @user_session.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
  <p>
    <%= f.label :login %><br />
    <%= f.text_field :login %>
  </p>
  <p>
    <%= f.label :password %><br />
    <%= f.password_field :password %>
  </p>
  <p><%= f.submit "Submit" %></p>
<% end %>

Thank you. 谢谢。

Using form_for(@user_session) alone will try to build out the path using a resource you have defined in your routes.rb . 单独使用form_for(@user_session)将尝试使用您在routes.rb定义的资源构建路径。 Which you currently don't have (I'm assuming, as you didn't mention it. Please correct if I'm wrong.). 你现在没有的(我假设,你没有提到它。如果我错了,请纠正。)。

A few ways to go.. 几种方法..

Add a resource and limit to the ones you need 添加资源并限制您需要的资源

resources :user_sessions, :only => [:create, :destroy]

These will use the default routing namings, but you can custom that up as you need. 这些将使用默认路由命名,但您可以根据需要自定义。


Match out the routes you need. 匹配您需要的路线。

match 'login' => 'user_sessions#create', :as => :post_login, :via => :post

View 视图

= form_for(@user_session), :url => post_login_path do |f|
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ActionController :: RoutingError(没有路由匹配“ / user_sessions /…”) - ActionController::RoutingError (No route matches "/user_sessions/…) Rails RoutingError(没有路由匹配[OPTIONS] - Rails RoutingError (No route matches [OPTIONS] RoutingError(Rails中没有路由与[GET]匹配 - RoutingError (No route matches [GET] in Rails Rails 4 RoutingError:没有路由匹配[POST] - Rails 4 RoutingError: No Route Matches [POST] 控制器使用authlogic测试期望user_sessions表 - Controller tests with authlogic expecting user_sessions table rails3 devise - 没有路由匹配“/ sessions / user” - rails3 devise - no route matches “/sessions/user” 设计命名空间 - ActionController :: RoutingError(无路由匹配{:action =>“new”,:controller =>“devise / sessions”}) - Devise in namespace - ActionController::RoutingError (No route matches {:action=>“new”, :controller=>“devise/sessions”}) Rails路线不一致和ActionController :: RoutingError-没有路线匹配 - Rails route inconsistency and ActionController::RoutingError - No route matches Rails教程中的“ RoutingError”和“ No route match…” - “RoutingError” and “No route matches…” from Rails Tutorial Rails:ActionController :: RoutingError没有路线匹配错误 - Rails: ActionController::RoutingError No route matches error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM