在会话控制器上设置:remember_me

时间:2017-02-14 12:44:58

标签: ruby-on-rails

我一直在关注迄今为止效果很好的Michael Hartl rails教程,但我遇到了一个似乎无法解决的问题。

我已将表单从 form_for 更新为 form_tag ,并且必须修改我的sessions_controller。

sessions_controller.rb

class SessionsController < ApplicationController
  layout "sessions"

  def new
  end

  def create
    user = User.find_by(email: params[:email].downcase)
    if user && user.authenticate(params[:password])
      log_in user
      params[:session][:remember_me] == '1' ? remember(user) : forget(user)
      redirect_to root_path
    else
      flash.now[:danger] = 'Invalid email/password combination'
      render 'new'
    end
  end

  def destroy
    log_out if logged_in?
    redirect_to root_path
  end
end

/sessions/new.html

<%= form_tag({ url: login_path }, { class: "form-horizontal" }) do %>
            <div class="form-group">
              <div class="col-lg-24">
                <div class="floatl hasFloatLabel">
                  <%= label_tag :email, "Email address", class: "floatl__label" %>
                  <%= email_field_tag :email, params[:email], placeholder: "Email address", class: "form-control floatl__input" %>
                </div>
              </div>
            </div>
            <div class="form-group margin-bottom-sm">
              <div class="col-lg-24">
                <div class="floatl hasFloatLabel">
                  <%= label_tag :password, "Password", class: "floatl__label" %>
                  <%= password_field_tag :password, nil, placeholder: "Password", class: "form-control floatl__input" %>
                  <em><a href="#">Forgot your Password?</a></em>
                </div>
              </div>
            </div>
            <div class="form-group margin-bottom-sm">
              <div class="col-lg-24">
                <div class="floatl hasFloatLabel">
                  <%= check_box_tag :remember_me, nil, placeholder: "Remember Me", class: "form-control floatl__input" %>
                  <p class="remember-me">Remember me?</p>
                </div>
              </div>
            </div>
            <div class="form-group">
              <div class="col-lg-24">
                <%= submit_tag "Login", class: "btn btn-brand" %>
              </div>
            </div>
          <% end %>

尝试设置remember_token时似乎中断了,如果从代码中删除[:session],它会让我登录,但不会设置remember_token。

我是否错过了某些内容或是否有更简单的方法来设置此令牌?

sessions_helper.rb

module SessionsHelper

  # Logs in the given user.
  def log_in(user)
    session[:user_id] = user.id
  end

  # Remembers a user in a persistent session.
  def remember(user)
    user.remember
    cookies.permanent.signed[:user_id] = user.id
    cookies.permanent[:remember_token] = user.remember_token
  end

  # Returns the user corresponding to the remember token cookie.
  def current_user
    if (user_id = session[:user_id])
      @current_user ||= User.find_by(id: user_id)
    elsif (user_id = cookies.signed[:user_id])
      user = User.find_by(id: user_id)
      if user && user.authenticated?(cookies[:remember_token])
        log_in user
        @current_user = user
      end
    end
  end

  # Returns true if the user is logged in, false otherwise.
  def logged_in?
    !current_user.nil?
  end

  # Forgets a persistent session.
  def forget(user)
    user.forget
    cookies.delete(:user_id)
    cookies.delete(:remember_token)
  end

  # Logs out the current user.
  def log_out
    forget(current_user)
    session.delete(:user_id)
    @current_user = nil
  end
end

0 个答案:

没有答案