Ruby on rails 如何将变量从过滤器前传递到视图

Ruby on rails 如何将变量从过滤器前传递到视图,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我想知道如何将“当前日”和“已过日”传递到我的视图中 class PostController < ApplicationController before_filter :detect_current_day def index #some logic end private def detect_current_day if (params[:year] && params[:month] && params[:da

我想知道如何将“当前日”和“已过日”传递到我的视图中

class PostController < ApplicationController
  before_filter :detect_current_day

  def index
    #some logic
  end

 private

 def detect_current_day
    if (params[:year] && params[:month] && params[:day])
      @current_day =  Date.parse params.values_at(:year, :month, :day).join('.')
    else
      @current_day = Date.today;
    end

    # Check if day already exist
    @hasToday = Post.created_on(@current_day).count > 0 ? true : false;
  end

end
class PostController0?真:假;
结束
结束

谢谢

您做得很正确,但是您期望的实例变量是错误的。您定义了@current_day和@hasToday。您将看到它。如果您想要@has_day。请在您的before筛选器中初始化

@has_day = Post.created_on(@current_day).count > 0 ? true : false;

我看不出您的代码有任何错误,但您的@current_day是日期格式的,如果您想在视图中打印出来,需要将其转换为:

<%= @current_day.strftime("%Y %m %d") %>


您应该已经做了很多事情:设置实例变量,这就足够了。你的错误是什么?注意你的某个地方有一个打字错误。你的问题提到了has_day,但你的代码显示hasToday。