登录失败,没有路由匹配错误

时间:2013-04-29 13:37:31

标签: mysql ruby-on-rails ruby login

我正在尝试学习如何使用Rails编写登录系统,但我觉得我现在正在攻击这个,我遵循了这个指南:

http://rubysource.com/rails-userpassword-authentication-from-scratch-part-ii/

但我不确定它是否是为Rails 4和Ruby 2开发的。

我一直收到这个错误:

Rails Framework Error

我感觉这与会话的控制器有关,但我不完全确定所以我会包含所有文件。

User.rb

   class User < ActiveRecord::Base
    attr_accessor :username, :email, :password, :password_confirmation
    EMAIL_REGEX = /\A[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]+\z/
    validates :username, :presence => true, :uniqueness => true, :length => { :in => 3..20 }
    validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
    validates :password, :confirmation => true #password_confirmation attr
    validates_length_of :password, :in => 6..20, :on => :create

    before_save :encrypt_password
    after_save :clear_password

    def encrypt_password
        if password.present?
            self.salt = BCrypt::Engine.generate_salt
            self.encrypted_password= BCrypt::Engine.hash_secret(password, salt)
        end
    end

    def clear_password
        self.password = nil
    end
    def self.authenticate(username_or_email="", login_password="")
        if EMAIL_REGEX.match(username_or_email)
            user = User.find_by_email(username_or_email)
        else
            user = User.find_by_username(username_or_email)
        end

        if user && user.match_password(login_password)
            return user
        else
            return false
        end
    end

    def match_password(login_password="")
        encrypted_password == BCrypt::Engine.hash_secret(login_password, salt)
    end
end

users_controller.rb

class UsersController < ApplicationController
    before_filter :save_login_state, :only => [:new, :create]
    def new
        @user = User.new
    end
    def create
        @user = User.new(user_params)
        if @user.save
            flash[:notice] = "You Signed up successfully"
            flash[:color]= "valid"
        else
            flash[:notice] = "Form is invalid"
            flash[:color]= "invalid"
        end
        render "new"
    end
    def user_params
        params.require(:user).permit(:username, :email, :password, :password_confirmation)
    end
end

sessions_controller.rb

class SessionsController < ApplicationController

    before_filter :authenticate_user, :only => [:home, :profile, :setting]
    before_filter :save_login_state, :only => [:login, :login_attempt]

    def login

    end

    def login_attempt
        authorized_user = User.authenticate(params[:username_or_email],params[:login_password])
        if authorized_user
            session[:user_id] = authorized_user.id
            flash[:notice] = "Wow Welcome again, you logged in as #{authorized_user.username}"
            redirect_to(:action => 'home')
        else
            flash[:notice] = "Invalid Username or Password"
            flash[:color]= "invalid"
            render "login"  
        end
    end
    def logout
        session[:user_id] = nil
        redirect_to :action => 'login'
    end

application_controller.rb

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  protected
    def authenticate_user
        unless session[:user_id]
            redirect_to(:controller => 'sessions', :action => 'login')
            return false
        else
            # set current user object to @current_user object variable
            @current_user = User.find session[:user_id]
            return true
        end
    end

    def save_login_state
        if session[:user_id]
            redirect_to(:controller => 'sessions', :action => 'home')
            return false
        else
            return true
        end
    end
end

login.html.erb

<% @page_title = "UserAuth | Login" -%>
<div class= "Sign_Form">
    <h1>Log in</h1>
    <%= form_tag :sessions => :login_attempt do %>
        <p>Username or Email:</br> <%= text_field_tag(:username_or_email) %></p>
        <p>Password:</br> <%= password_field_tag :login_password %></p>
        <%= submit_tag("Log In") %>
    <% end %>
</div>

的routes.rb

BillingSystem::Application.routes.draw do
    root :to => "sessions#login"
    match "signup", :to => "users#new", via: [:get, :post]
    match "login", :to => "sessions#login", via: [:get, :post]
    match "logout", :to => "sessions#logout", via: [:get, :post]
    match "home", :to => "sessions#home", via: [:get, :post]
    match "profile", :to => "sessions#profile", via: [:get, :post]
    match "setting", :to => "sessions#setting", via: [:get, :post]
end

3 个答案:

答案 0 :(得分:0)

你正在使用rails 4和Ruby 2,对吗?

  1. match不再捕获所有路线,请尝试get

  2. 看看Devise gem

答案 1 :(得分:0)

问题是您将表单发布到(root_url或/)的URL不处理发布请求(通常不应该)。表单将发布到root,因为您没有指定URL,因此它默认为当前的(root)以将表单发布到。从你的路线,它应该类似于:

<% @page_title = "UserAuth | Login" -%>
<div class= "Sign_Form">
    <h1>Log in</h1>
    <%= form_tag "/login", :sessions => :login_attempt do %>
        <p>Username or Email:</br> <%= text_field_tag(:username_or_email) %></p>
        <p>Password:</br> <%= password_field_tag :login_password %></p>
        <%= submit_tag("Log In") %>
    <% end %>
</div>

请参阅api doc以获取更多与此相关的内容:http://apidock.com/rails/ActionView/Helpers/FormTagHelper/form_tag

另一个注意事项,在你对rails更加熟悉之前我不建议你研究一下Devise,他们实际上会反对它。 https://github.com/plataformatec/devise#starting-with-rails

答案 2 :(得分:0)

这对我有用,我尝试了同样的教程。 root:to =&gt; “会话#登录”
得到“注册”,:to =&gt; “用户#新”
得到“登录”,:to =&gt; “会话#登录”
发布“login_attempt”,:to =&gt; “会话#login_attempt”
得到“注销”,:to =&gt; “会话#注销”
得到“家”,:to =&gt; “会话#家”
得到“个人资料”,:to =&gt; “会话#配置文件”
得到“设置”,:to =&gt; “会话#设置”
资源:用户