【问题标题】:Unable to destroy session object in Heroku无法在 Heroku 中销毁会话对象
【发布时间】:2016-07-12 14:23:44
【问题描述】:

我在跟踪以下问题的修复时遇到了挑战:

  • 我可以在我的开发环境中登录/注销

  • 我无法在生产环境中注销

我查看并尝试了以下问题的解决方案:

我注意到 Heroku 日志中的 SQL 说选择已删除_at 为空的员工,否则当我单击“注销”时只会看到对会话#new 的 GET 请求。

这是我的代码:

sessions_controller.rb

class SessionsController < ApplicationController
  skip_before_action :require_login, except: [:destroy]

  def new
    @employee = Employee.new
  end

  def create
    if @employee = login(params[:email], params[:password])
      flash[:success] = "You're logged in!"
      redirect_back_or_to(root_path)
    else
      @employee = Employee.new
      flash.now[:notice] = "Login failed."
      render :new
    end
  end

  def destroy
    logout
    flash.now[:notice] = "You have successfully logged out."
    redirect_to(root_path)
  end
end

_nav.html.erb

<li>
  <% if current_employee.present? %>
     <% if current_employee.is_admin %>
      <h5 style="margin-top: 5%;">
        Logged in as <strong><em><%= current_employee.email %></em></strong>
        <%= link_to('Edit Account', edit_account_path(current_employee.account_id), class: "text-normal") %>
        - or -
        <%= link_to('Logout', logout_path, options = { method: :delete, class: "text-normal" }) %>
      </h5> 
    <% elsif current_employee.is_admin == false %>
      <h5 style="margin-top: 5%;">
        Logged in as <strong><em><%= current_employee.email %></em></strong>
        <%= link_to('Edit Profile', edit_employee_path(current_employee), class: "text-normal") %> 
        - or - 
        <%= link_to('Logout', logout_path, options = { method: :delete, class: 'text-normal' }) %>
      </h5>
    <% end %>
  <% else %>
    <h5 style="margin-top: 9.5%;">
      &nbsp;&nbsp; | &nbsp;&nbsp;&nbsp;&nbsp;
      <%= link_to('Login', login_path, id: "employee_login", class: "text-normal") %>
      - or - 
      <%= link_to('Register Today!', new_account_path, id:"admin_registration", class: "text-normal") %>
    </h5>
  <% end %>     
</li>

routes.rb

Rails.application.routes.draw do
  root to: "static_pages#home"

  get "static_pages/about", to: "static_pages#about", as: :about
  get "static_pages/pricing", to: "static_pages#pricing", as: :pricing
  get "static_pages/contact", to: "static_pages#contact", as: :contact

  get "sessions", to: "sessions#new", as: :login
  post "sessions", to: "sessions#create"
  delete "sessions", to: "sessions#destroy", as: :logout
  resources :employees 
end

config/production.rb(添加了这些配置)

Rails.application.configure do
  config.cache_classes = true
  config.assets.compile = true
  config.assets.digest = true
end

提前感谢您自愿提供新的眼睛!

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4


    【解决方案1】:

    原来我解决了这个问题,处理了一个不同的问题。

    我从 production.rb 中删除了 config.action_controller.asset_host = "http://www.name_of_site.com",以便让我的资产在生产中工作。

    我不确定为什么,但这也解决了无法在 Heroku 上注销的问题。

    我将此作为答案发布,希望它能帮助将来处理类似问题的人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-13
      • 2023-03-19
      • 1970-01-01
      相关资源
      最近更新 更多