Ruby on rails 无法使用身份验证功能“登录”或“注册”(无Desive gem)
我目前无法通过我的应用程序注册或登录用户,我也无法找出原因。我在用户/配置文件模型中使用嵌套参数 当我尝试注册一个新用户时,我会收到一条正确的闪信,上面写着 无效的电子邮件或密码 我从头开始创建身份验证,没有使用Deviate!。我还有一个“忘记密码”/“记住我”功能,但我没有在下面显示这些信息,因为我认为这些信息与此无关 这是控制台日志,似乎是回滚,但没有给出具体错误: {参数:{utf8=>✓, 真实性\令牌=>Ek4cgnR3FQePCg/A4Wqc3atinU+WwRNgj+5hpXsd4mY=,用户=>{email=>sign@up.co,password=>[FILTERED],password\u confirmation=>[FILTERED],profile\u attributes=>{first\u name=>101001010,last\u name=>101011101101,linkedin=>www.linkedin.com/signup,twitter=>www.twitter.com/signup},commit=>signup} 0.1ms开始事务 用户存在0.1ms从users.email='1'中选择1作为一个用户sign@up.co“限制1 0.1ms回滚事务 呈现用户/new.html.haml在布局/应用程序中60.3ms 在158ms视图中完成200 OK:75.9ms | ActiveRecord:0.3ms} models/user.rb **视图/会话/new.html.hamlRuby on rails 无法使用身份验证功能“登录”或“注册”(无Desive gem),ruby-on-rails,authentication,Ruby On Rails,Authentication,我目前无法通过我的应用程序注册或登录用户,我也无法找出原因。我在用户/配置文件模型中使用嵌套参数 当我尝试注册一个新用户时,我会收到一条正确的闪信,上面写着 无效的电子邮件或密码 我从头开始创建身份验证,没有使用Deviate!。我还有一个“忘记密码”/“记住我”功能,但我没有在下面显示这些信息,因为我认为这些信息与此无关 这是控制台日志,似乎是回滚,但没有给出具体错误: {参数:{utf8=>✓, 真实性\令牌=>Ek4cgnR3FQePCg/A4Wqc3atinU+WwRNgj+5hpXsd
%h1 Sign In
= simple_form_for "", url: sessions_path, html: {class: "form-horizontal"} do |f|
= f.input :email
= f.input :password
= f.submit "Sign In", class: "btn btn-primary"
%br
%p
=link_to "Forgot your password?", new_password_reset_path
%p
No account, yet?
= link_to "Sign up", signup_path
这个问题困扰了我好长时间了。我想测试一些用户功能,但我不能这样做,因为我无法登录。现在数据库中只有一个用户记录,我在控制台中手动创建了它。我也无法登录到该用户记录
非常感谢您的帮助
def create
@user = User.new(user_attributes)
if @user.save && user.authenticate(params[:password])
session[:user_id] = user.id
...
end
在这段代码中,您创建了一个实例变量@user,但调用了authenticate on user。您应该使用User.authenticate或@User.authenticate,具体取决于您在模型中实现身份验证方法的方式。您还应该将会话[:user\u id]=user.id更改为会话[:user\u id]=@user.id
class UsersController < ApplicationController
def new
@user = User.new
@user.build_profile
end
def create
@user = User.new(user_attributes)
if @user.save && user.authenticate(params[:password])
session[:user_id] = user.id
redirect_to root_url, notice: "Thanks for signing in"
else
flash.now.alert = "Invalid email or password"
render :new
end
end
private
def user_attributes
params.require(:user).permit(:email, :password,
:password_confirmation,
{profile_attributes: [:first_name, :last_name, :linkedin, :twitter]})
end
end
class ProfilesController < ApplicationController
before_action :find_profile
def show
end
def update
if @profile.update_attributes(profile_params)
redirect_to posts_path
end
end
def edit
end
private
def profile_params
params.require(:profile).permit(:first_name, :last_name, :linkedin, :twitter)
end
def find_profile
@profile = current_user.profile
end
end
class SessionsController < ApplicationController
def create
user = User.find_by_email(params[:email])
respond_to do |format|
if user && user.authenticate(params[:password])
if params[:remember_me]
cookies.permanent[:auth_token] = user.auth_token
else
cookies[:auth_token] = user.auth_token
end
format.html { redirect_to root_path, notice: "You're logged in!" }
format.js do
flash.now.notice = "You're signed in!"
render
end
else
flash.now.alert = "Email or password is invalid"
format.html { render :new }
format.js { render :new }
end
end
end
def destroy
session[:user_id] = nil
redirect_to root_url, notice: "Logged out!"
end
end
%h1 Sign Up for a New Account
= simple_form_for @user, html: { class: 'form-horizontal' } do |f|
.well
.container
= f.input :email, html_input: {class: "form-control"}
= f.input :password
= f.input :password_confirmation
= f.fields_for :profile do |p|
= p.input :first_name
= p.input :last_name
= p.input :linkedin
= p.input :twitter
= label_tag :remember_me
= check_box_tag :remember_me, 1, params[:remember_me]
= f.submit "Sign Up", class: "btn btn-primary"
%h1 Sign In
= simple_form_for "", url: sessions_path, html: {class: "form-horizontal"} do |f|
= f.input :email
= f.input :password
= f.submit "Sign In", class: "btn btn-primary"
%br
%p
=link_to "Forgot your password?", new_password_reset_path
%p
No account, yet?
= link_to "Sign up", signup_path
def create
@user = User.new(user_attributes)
if @user.save && user.authenticate(params[:password])
session[:user_id] = user.id
...
end