Ruby on rails 为Rails中的布局类实例变量选择控制器

Ruby on rails 为Rails中的布局类实例变量选择控制器,ruby-on-rails,layout,controller,partial-views,class-instance-variables,Ruby On Rails,Layout,Controller,Partial Views,Class Instance Variables,我有一个DateTime类实例变量@current\u year=DateTime.now.year,我已经将其设置为一个页脚部分。这部分内容在我的几个客户端页面上呈现(并且跨多个控制器,如页面主体中的,layout/application.html.erb页面是为站点导航保留的,因此格式不适合它。虽然我可以为控制器中出现的每个页面方法声明它,但我会发现一些更干燥的内容。有没有一个地方可以是否定义要在我的布局组件中调用的我的时间变量?您可以在应用程序控制器中添加设置年份操作,例如: class

我有一个DateTime类实例变量
@current\u year=DateTime.now.year
,我已经将其设置为一个页脚部分。这部分内容在我的几个客户端页面上呈现(并且跨多个控制器,如页面主体中的
,layout/application.html.erb页面是为站点导航保留的,因此格式不适合它。虽然我可以为控制器中出现的每个页面方法声明它,但我会发现一些更干燥的内容。有没有一个地方可以是否定义要在我的布局组件中调用的我的时间变量?

您可以在
应用程序控制器
中添加
设置年份
操作,例如:

class ApplicationController < ActionController::Base 

  private 

    def set_year 
      @current_year = DateTime.now.year
    end

end
class FooController < ApplicationController
  before_action :set_year, only: [:some_action, :another_action]
end
class ApplicationController < ActionController::Base

  def current_year
    @current_year ||= DateTime.now.year
  end

end
module ApplicationHelper

  def current_year
    @current_year ||= DateTime.now.year
  end

end
或者,你可以在
ApplicationController
中创建一个
current\u year
操作,比如:

class ApplicationController < ActionController::Base 

  private 

    def set_year 
      @current_year = DateTime.now.year
    end

end
class FooController < ApplicationController
  before_action :set_year, only: [:some_action, :another_action]
end
class ApplicationController < ActionController::Base

  def current_year
    @current_year ||= DateTime.now.year
  end

end
module ApplicationHelper

  def current_year
    @current_year ||= DateTime.now.year
  end

end
我认为,将
本年度
移动到助手中的好处是它会消除
应用程序控制器
的混乱。我进一步认为,缺点是它(在某种程度上)会破坏
本年度
操作的来源。同样,你必须考虑你的船只浮动系数

顺便说一句,
@current\u year
不是一个
DateTime
。它是一个
Fixnum

您可以在
应用程序控制器中添加一个
set\u year
操作,类似于:

class ApplicationController < ActionController::Base 

  private 

    def set_year 
      @current_year = DateTime.now.year
    end

end
class FooController < ApplicationController
  before_action :set_year, only: [:some_action, :another_action]
end
class ApplicationController < ActionController::Base

  def current_year
    @current_year ||= DateTime.now.year
  end

end
module ApplicationHelper

  def current_year
    @current_year ||= DateTime.now.year
  end

end
或者,你可以在
ApplicationController
中创建一个
current\u year
操作,比如:

class ApplicationController < ActionController::Base 

  private 

    def set_year 
      @current_year = DateTime.now.year
    end

end
class FooController < ApplicationController
  before_action :set_year, only: [:some_action, :another_action]
end
class ApplicationController < ActionController::Base

  def current_year
    @current_year ||= DateTime.now.year
  end

end
module ApplicationHelper

  def current_year
    @current_year ||= DateTime.now.year
  end

end
我认为,将
本年度
移动到助手中的好处是它会消除
应用程序控制器
的混乱。我进一步认为,缺点是它(在某种程度上)会破坏
本年度
操作的来源。同样,你必须考虑你的船只浮动系数


顺便说一句,
@current\u year
不是
DateTime
。它是一个
Fixnum

您可以在application\u helper.rb文件中创建一个方法 可以在你喜欢的任何地方使用

def current_year
  @current_year = DateTime.now.year
end

您可以在application_helper.rb文件中创建一个方法 可以在你喜欢的任何地方使用

def current_year
  @current_year = DateTime.now.year
end

或者一个助手,有点像
当前用户
是如何定义的:
定义当前年;@current\u year |=DateTime.now.year;在
应用程序助手
中结束
。或者一个助手,有点像
当前用户
是如何定义的:
定义当前年;@current\u year | DateTime.now.year;在
应用程序HEL中结束
根据
。我使用了另一种方法,因为我的船浮系数必须考虑到采购和初级开发人员不一定熟悉助手,但出于未来目的,我真的很喜欢这个答案!我当前的应用程序控制器有两种方法,所以在这种情况下没有太大问题。:)我使用了另一种方法,因为我的船浮系数必须考虑到来源和初级开发人员不一定熟悉助手,但出于未来目的,我非常喜欢这个答案!我当前的应用程序控制器有两种方法,因此在这种情况下没有太大问题。:)