[英]how to create users and log in/out sessions using ruby on rails
I am new to rails and I am trying to build a simple web site where it requires creating users that are able to log in and out but when creating the user I get ArgumentError in UsersController#create wrong number of arguments (2 for 1) and this happens also when I try to log in where I get ArgumentError in SessionsController#create wrong number of arguments (2 for 1) 我是Rails的新手,我正在尝试构建一个简单的网站,在该网站中它需要创建能够登录和注销的用户,但是在创建用户时,我在UsersController中收到ArgumentError#创建错误的参数数量(2为1),并且当我尝试登录到在SessionsController中获取ArgumentError的地方时也会发生这种情况#创建错误数量的参数(2为1)
the model for the user is as follows 用户模型如下
Class User < ActiveRecord::Base
attr_accessor :password
#validation for password on creation.
validates :password, :presence => true,
:confirmation => true,
:length => {:within => 6..40},
:on => :create
#validation for password on update, forget password option
validates :password, :confirmation => true,
:length => {:within => 6..40},
:on => :update
validates :user_name, presence: true,
uniqueness: true
validates :status, presence: true
before_save :encrypt_password
def encrypt_password
if password.present?
self.password_salt = BCrypt::Engine.generate_salt
self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
end
end
def self.authenticate(user_name, password)
user = find_by_user_name(user_name)
if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
#authenticate
user
else
# la2 :(
nil
end
end
end
and the user controller : 和用户控制器:
class UsersController < ApplicationController
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
redirect_to root_url, :notice => "Signed up!"
else
render "new"
end
end
private
def user_params
params.require(:user).permit(:user_name, :status, :password, :password_confirmation)
end
end
and regarding the sessions it has no model where the session controller is as follows : 对于会话,它没有模型,其中会话控制器如下:
class SessionsController < ApplicationController
def new
end
def create
user = User.authenticate(params[:user_name], params[:password])
if user
session[:user_id] = user.id
redirect_to root_url, :notice => "Logged in!"
else
flash.now.alert = "Invalid email or password"
render "new"
end
end
def destroy
session[:user_id] = nil
redirect_to root_url, :notice => "Logged out!"
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.