登录/注册功能不起作用,@ current_user不起作用,会话不起作用 [英] Login/signup function not working, @current_user not working, sessions not working

查看:137
本文介绍了登录/注册功能不起作用,@ current_user不起作用,会话不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Rails的新手,并且正在开发一个涉及简单登录功能的练习应用程序.我一直在遵循CodeAcademy编写的教程,但是这些代码在很多方面都无法正常工作.首先,即使Rails在与会话声明共享的"if"块内执行其余代码,也不会设置会话(顺便说一句,不会返回任何错误).

I'm new to Rails, and am working on a practice app that involves a simple login function. I've been following a tutorial from CodeAcademy by the books, however the code is not working in quite a few ways. First of all, the sessions do not set, even though Rails is executing the rest of the code inside the "if" block shared with the session declaration (btw, no errors are returned).

会话控制器:

class SessionsController < ApplicationController
  def new

  end

 def create
  @user = User.find_by_username(params[:session][:name])
  if @user && @user.authenticate(params[:session][:password])
    session[:user_id] = @user.id
    redirect_to '/posts'
  else
    session[:user_id] = nil
    flash[:warning] = "Failed login- try again"
    redirect_to '/login'
  end 
end

  def destroy
session[:session_id] = nil 
redirect_to login_path 
  end
end 

从该问题推断出,我的"current_user"功能不起作用,这很可能是因为未设置会话.

Extrapolating from that issue, my "current_user" function is not working, which is most likely because the session is not being set.

应用程序控制器:

class ApplicationController < ActionController::Base
def current_user
    return unless session[:user_id]
    @current_user ||= User.find(session[:user_id])
end

def require_user 
  redirect_to '/login' unless current_user 
end

end

我们非常感谢您的帮助.让我知道您是否还需要查看其他内容.

Any help is much appreciated. Let me know if you need to see anything else.

注意:我知道我应该使用Devise,并且我计划在以后的项目中进行更严肃的工作.但是,就像我说的那样,这是一个练习/测试应用程序,可以帮助开发我的编码技能,并且在使用像Devise这样的神奇"瑰宝之前,我希望获得亲自制作登录系统的动手经验.

NOTE: I know I should use Devise, and I am planning to in my future, more serious projects. However, like I said, this is a practice/test app to help develop my coding skills, and before using a "magical" gem like Devise I want to get hands-on experience with making a login system myself.

推荐答案

有一些可能的问题.

首先,在调用current_user方法之前不会设置@current_user.就像@Neha指出的那样,您需要在ApplicationController中添加一个辅助方法,以便您所有的视图都可以访问current_user方法.将此行添加到您的ApplicationController中:

First, @current_user is not set until the current_user method is called. And as @Neha pointed out, you'll need to add a helper method to your ApplicationController so that all your views will have access to the current_user method. Add this line to your ApplicationController:

helper_method :current_user

现在,要诊断问题,让我们进行一些设置,使您可以对会话和current_user有所了解.

Now, to diagnose the problem, let's set something up that lets you get some visibility into your session and current_user.

首先,在views/layouts/application.html.erb<= yield %>行之后,添加以下内容:

First, in views/layouts/application.html.erb, just after the line that says <= yield %>, add this:

<%= render 'layouts/footer' %>

然后添加一个新文件views/layouts/_footer.html.erb:

<hr/>
Session:<br/>
<% session.keys.each do |key| %>
  <%= "#{key}: #{session[key]}" %><br/>
<% end %>
<br/>
User:<br/>
<%= current_user&.username || '[None]' %>

现在,在每个视图的底部,您都可以看到会话的详细信息.

Now at the bottom of every view you can see the details of your session.

sessions#create操作中,找到用户存在潜在的问题.您正在使用params[:session][:name],而您可能应该使用params[:session][:username].

In your sessions#create action, you have a potential problem with finding your User. You are using params[:session][:name] where you probably should be using params[:session][:username].

相切地,销毁会话的正确方法不是将session[:id]设置为nil,而是使用reset_session.因此,您的SessionsController应该如下所示:

Also, tangentially, the proper way to destroy a session is not by setting session[:id] to nil, but instead to use reset_session. So your SessionsController should look like this:

class SessionsController < ApplicationController
  def new
  end

  def create
    @user = User.find_by_username(params[:session][:username])
    if @user && @user.authenticate(params[:session][:password])
      session[:user_id] = @user.id
      redirect_to '/posts'
    else
      session[:user_id] = nil
      flash[:warning] = "Failed login- try again"
      redirect_to '/login'
    end
  end

  def destroy
    reset_session
    redirect_to login_path
  end
end

这篇关于登录/注册功能不起作用,@ current_user不起作用,会话不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆