Howto pass vars from before_filter to view
I was wondering how to pass @current_day and @has_day to my view?
class PostContro开发者_运维知识库ller < 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
Thanks
you have done correctly but you are expecting wrong instance variables. you defined @current_day and @hasToday. you will get it in view.if you want @has_day.please initialize in your before filter
@has_day = Post.created_on(@current_day).count > 0 ? true : false;
I don't see anything wrong with your code, but your @current_day is in date format, and if you want to print it out in your view you need to convert it to:
<%= @current_day.strftime("%Y %m %d") %>
精彩评论