UsersController中的NoMethodError#在ROR-4中登录

时间:2014-12-18 05:08:44

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

请帮我解决此错误。当我点击login link时,该页面给了我以下错误。

错误:

undefined method `[]' for nil:NilClass

Extracted source (around line #22):       

    20 end
    21 def login
    22 authorized_user = User.authenticate(params[:user][:username],params[:user][:password])
    23 if authorized_user
               24  session[:user_id] = authorized_user.id
                25 flash[:notice] = "Wow Welcome again, you logged in as #{authorized_user.username}"

我的代码片段如下所示。

视图/用户/ index.html.erb:

<h1>This is Registration page..</h1>
<div class="sign">
<%= link_to "Registration", users_new_path ,id: "btn"%>
<%= link_to "Login", users_login_path ,id: "btn1"%>
</div>

视图/用户/ new.html.erb:

<h1>Register here</h1>
<div class= "Sign_Form">
<%= form_for @user ,:url => { :action => "create" } do |f|%>
<p>
<%= f.label :NAME %>
<%= f.text_field :name,placeholder:"Enter your name"%>
</p>
<p>
<%= f.label :EMAIL %>
<%= f.text_field :email,placeholder:"Enter your email"%>
</p>
<p>
<%= f.label :PASSWORD %>
<%= f.password_field :password,placeholder:"Enter your password"%>
</p>
<p>
<%= f.submit "SIGN UP"%>
</p>
<% end %>
<% if @user.errors.any? %>
    <ul class="Signup_Errors">
        <% for message_error in @user.errors.full_messages %>
            <li><%= message_error %></li>
        <% end %>
    </ul>
  <% end %>
  </div>

视图/用户/ login.html.erb

<h1>Login here</h1>
<div class= "Sign_Form">
<h1>Log in</h1>
<%= form_for @user,:url => { :action => "login" } do |f|  %>
<p>
UserName or Email: <%= f.text_field :username,placeholder:"Enter your user name or email" %>
</p>
<p>
Password : <%= f.password_field :password,placeholder:"Enter your valid password"%>
</p>
<p>
<%= f.submit "LOGIN" %>
</p>
<% end %>
</div>

控制器/ users_controller.rb:

class UsersController < ApplicationController
def index
end
def new
@user=User.new
end
def login
end
def create
@user=User.new(user_params)
if @user.save
        flash[:notice] = "You Signed up successfully"
        flash[:color]= "valid"
        redirect_to :action => 'index'
else
        flash[:notice] = "Form is invalid"
        flash[:color]= "invalid"
        redirect_to :action => 'new'
end
end
def login
authorized_user = User.authenticate(params[:user][:username],params[:user][: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"
            redirect_to :action => 'login'
end
end
def home
end
def user_params
params.require(:user).permit(:name, :email,:password)
end
end

模型\ user.rb:

class User < ActiveRecord::Base
 EMAIL_REGEX = /\A[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\z/i
 validates :name, :presence => true, :uniqueness => true, :length => { :in => 3..20 }
 validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
 validates :password, :confirmation => true
 validates_length_of :password, :in => 6..20, :on => :create
 def self.authenticate(username="", password="")

    if  EMAIL_REGEX.match(username)    
      user = User.find_by_email(username)
    else
      user = User.find_by_name(username)
    end

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

配置/ route.rb:

Rails.application.routes.draw do
  root "users#index"
  get "users/new" => "users#new"
  get "users/login" => "users#login"
  post "users/create" => "users#create"
  post "users/login" => "users#login"
  get "users/home" => "users#home"
end

用户/ lauout / application.html.erb:

<!DOCTYPE html>
<html>
<head>
  <title>Validation</title>
  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>
</head>
<body>
<% if flash[:color]== "valid" %>
        <div class="valid_notice">
            <p><%= flash[:notice]%></p>
        </div>
    <% elsif flash[:color]== "invalid"%>
        <div class="invalid_notice">
            <p><%=flash[:notice]%></p>
        </div>
    <%else%>
        <div class="notice">
            <p><%=flash[:notice]%></p>
        </div>
    <%end%>
<%= yield %>

</body>
</html>

我正在使用rails-4。请帮我解决这个问题。提前谢谢。

2 个答案:

答案 0 :(得分:1)

试试这个:

将表单更改为:

<h1>Login here</h1>
<div class= "Sign_Form">
<h1>Log in</h1>
<%= form_for @user,:url => { :action => "authenticate" } do |f|  %>
<p>
UserName or Email: <%= f.text_field :username,placeholder:"Enter your user name or email" %>
</p>
<p>
Password : <%= f.password_field :password,placeholder:"Enter your valid password"%>
</p>
<p>
<%= f.submit "LOGIN" %>
</p>
<% end %>
</div>

将此路线post "users/login" => "users#login"更改为post "users/authenticate" => "users#authenticate"

第二次登录方法:

def authenticate
authorized_user = User.authenticate(params[:user][:username],params[:user][: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"
            redirect_to :action => 'login'
end
end

告诉我它是否有效或问题是否仍然存在。

答案 1 :(得分:1)

link_to "Login", users_login_path(:user => {username: "username" , password: "user_password" })

试试这个