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)
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
It's generally a bad idea to roll your own security.
I would highly recommend checking out Devise
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.