如何在 Ruby/Rails 中显示 session 变量的值?

How do I display the value of a session variable in Ruby/Rails?

首先我不知道我是否正确地声明了一个 session 变量。另外,我把它放在应用程序控制器中,因为我知道它会 运行。如果有更好的地方放这个,请告诉我。期望的结果是用户 select 是他们希望在 session 期间将所有工作保存到的团队。然后,当 objects 被保存时,他们使用 session 变量:current_team 而不是:current_user。默认情况下,在创建时将为每个新用户创建一个团队。这就是为什么我要为每个用户查询第一个团队。在 session 期间,我计划让用户 select 他们 working/contributing 所在的团队,并且 selection 会一直存在,直到他们改变它。

application_controller.rb

class ApplicationController < ActionController::Base
  before_action :authenticate_user!
  before_action :set_current_team

  def set_current_team
    return unless session[:current_team]
    session[:current_team] = Team.first.where(user_id: current_user).id
  end


end

index.html.erb

<p><%= session[:current_team] %></p>

输出

Processing by PagesController#index as HTML
  User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" =  ORDER BY "users"."id" ASC LIMIT   [["id", 1], ["LIMIT", 1]]
  ↳ /home/dev/.rbenv/versions/2.5.2/lib/ruby/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98
  Rendering pages/index.html.erb within layouts/application
  Rendered pages/index.html.erb within layouts/application (0.5ms)
  Rendered shared/_navigation.html.erb (2.9ms)
Completed 200 OK in 172ms (Views: 168.3ms | ActiveRecord: 0.4ms)

我的模型是团队、用户和一个联接 table Team_member。 team_member.rb

  belongs_to :user
  belongs_to :team
  belongs_to :role
end

team.rb

  has_many :team_members
  has_many :users, through: :team_members
  has_many :roles, through: :team_members
end

一旦你有 session[:current_team],你可以将它保存到任何控制器中的变量中。例如在团队控制器中,你可以这样做:@current_team = session[:current_team]。然后在视图中你可以使用 @current_team.

我假设一个用户可以是多个团队的一部分,并且(显然)一个团队有很多用户,你的关系大致是这样的

class User < ApplicationRecord
  has_and_belongs_to_many :teams
end

class Team < ApplicationRecord
  has_and_belongs_to_many :users
end

您的方向已经正确,但我对 set_current_team 几乎没有做任何更改,并添加了访问器方法来获取当前团队实例。

class ApplicationController < ActionController::Base
  before_action :authenticate_user!
  before_action :set_current_team

  helper_method :current_team

  def set_current_team
    return unless session[:current_team]
    session[:current_team] = current_user.teams.first.id
  end

  def current_team
    @_team ||= Team.find(session[:current_team])
  end
end

current_team方法使用记忆技术在请求期间缓存当前团队,这样如果您在请求期间多次调用current_team,它不会再次调用数据库来获取团队记录。

强烈推荐阅读这篇文章4 Simple Memoization Patterns in Ruby

另请注意 set_current_team 上方的 helper_method 宏,它使此辅助方法也可用于您的视图。

因此以下内容将适用于您的索引

<p><%= current_team %></p>

以及所有控制器,因为所有控制器都继承自 ApplicationController

set_current_team 方法更新为

class ApplicationController < ActionController::Base
  before_action :authenticate_user!
  before_action :set_current_team

  # only set the session[:current_team] if it is nil (when user login)
  def set_current_team
    session[:current_team] ||= current_user.teams.first.id
  end

  # to access current_team
  def current_team
    @current_team ||= Team.find(session[:current_team])
  end
end

session[:current_team] 设置为用户在处理请求的 controller#action 中选择的团队 ID

当用户注销

时将session[:current_team]设置为nil