Rails 4: 在显示视图中按自定义属性过滤对象
Rails 4: filter objects by custom attributes in show view
在我的 Rails 4 应用程序中,我有以下模型:
class Calendar < ActiveRecord::Base
has_many :administrations
has_many :users, through: :administrations
has_many :posts
has_many :comments, through: :posts
end
class Administration < ActiveRecord::Base
belongs_to :user
belongs_to :calendar
end
class Post < ActiveRecord::Base
belongs_to :calendar
has_many :comments
end
post
个对象显示在它们所属日历的 Calendars#Show
视图中。
每个 post 都有一个 :date
自定义属性(不同于 :created_at
默认属性)。
我想在 Calendars#Show
视图中实现一个导航,这样我就可以按 :date
过滤 post 并逐月显示它们:
我已经开始实施如下:
在calendars_controller.rb
中,我有:
def show
@user = current_user
@calendar = @user.calendars.find(params[:id])
@current_month = params[:month].blank? ? Date.today.month : params[:month].to_i
@current_year = params[:year].blank? ? Date.today.year : params[:year].to_i
if @current_month == 13
@current_month = 1
@current_year = @current_year + 1
end
if @current_month == 0
@current_month = 12
@current_year = @current_year - 1
end
@posts = @calendar
.posts
.includes(:comments)
.where("Extract(month from date) = ?", @current_month)
.where("Extract(year from date) = ?", @current_year)
.order "date DESC"
# authorize @calendar
end
在日历 show.html.erb
文件中,我有:
<%= link_to '< Previous', calendar_path(@calendar, month: @current_month - 1) %>
<%= "#{Date::MONTHNAMES[@current_month]} #{@current_year}" %>
<%= link_to 'Next >', calendar_path(@calendar, month: @current_month + 1) %>
(然后我有一个循环来显示相关的 posts)。
上面的代码在当年运行良好,即我可以逐月导航,每个月我都会得到正确的 posts。
但是,当我尝试导航到上一年(点击“< Previous”按钮几次)或下一年(点击“< Previous”按钮几次)时,然后两个事情发生:
- 月份顺序从 2015 年 1 月到 2014 年 12 月到 2015 年 11 月(或从 2015 年 12 月到 2016 年 1 月到 2015 年 2 月),这意味着 @current_year 不再正确。
- 因此(这实际上是查询运行良好的标志),我再次得到相同的 posts,因为我没有导航到 2014 年 11 月,而是转到 2015 年 11 月,因此显示2015 年 11 月的 post(2016 年 2 月与 2015 年 2 月的问题相同)。
知道我的代码有什么问题吗?
我认为您也必须将 @current_year
传递给 calendar_path
。看起来这个条件总是将 2015 设置为当前年份。
@current_year = params[:year].blank? ? Date.today.year : params[:year].to_i
因为它适用于 12 月和 1 月,原因是您在月份为 0 or 13
时更改当前年份
这应该有效
<%= link_to '< Previous', calendar_path(@calendar, month: @current_month - 1, year: @current_year) %>
<%= link_to 'Next >', calendar_path(@calendar, month: @current_month + 1, year: @current_year) %>
在我的 Rails 4 应用程序中,我有以下模型:
class Calendar < ActiveRecord::Base
has_many :administrations
has_many :users, through: :administrations
has_many :posts
has_many :comments, through: :posts
end
class Administration < ActiveRecord::Base
belongs_to :user
belongs_to :calendar
end
class Post < ActiveRecord::Base
belongs_to :calendar
has_many :comments
end
post
个对象显示在它们所属日历的 Calendars#Show
视图中。
每个 post 都有一个 :date
自定义属性(不同于 :created_at
默认属性)。
我想在 Calendars#Show
视图中实现一个导航,这样我就可以按 :date
过滤 post 并逐月显示它们:
我已经开始实施如下:
在calendars_controller.rb
中,我有:
def show
@user = current_user
@calendar = @user.calendars.find(params[:id])
@current_month = params[:month].blank? ? Date.today.month : params[:month].to_i
@current_year = params[:year].blank? ? Date.today.year : params[:year].to_i
if @current_month == 13
@current_month = 1
@current_year = @current_year + 1
end
if @current_month == 0
@current_month = 12
@current_year = @current_year - 1
end
@posts = @calendar
.posts
.includes(:comments)
.where("Extract(month from date) = ?", @current_month)
.where("Extract(year from date) = ?", @current_year)
.order "date DESC"
# authorize @calendar
end
在日历 show.html.erb
文件中,我有:
<%= link_to '< Previous', calendar_path(@calendar, month: @current_month - 1) %>
<%= "#{Date::MONTHNAMES[@current_month]} #{@current_year}" %>
<%= link_to 'Next >', calendar_path(@calendar, month: @current_month + 1) %>
(然后我有一个循环来显示相关的 posts)。
上面的代码在当年运行良好,即我可以逐月导航,每个月我都会得到正确的 posts。
但是,当我尝试导航到上一年(点击“< Previous”按钮几次)或下一年(点击“< Previous”按钮几次)时,然后两个事情发生:
- 月份顺序从 2015 年 1 月到 2014 年 12 月到 2015 年 11 月(或从 2015 年 12 月到 2016 年 1 月到 2015 年 2 月),这意味着 @current_year 不再正确。
- 因此(这实际上是查询运行良好的标志),我再次得到相同的 posts,因为我没有导航到 2014 年 11 月,而是转到 2015 年 11 月,因此显示2015 年 11 月的 post(2016 年 2 月与 2015 年 2 月的问题相同)。
知道我的代码有什么问题吗?
我认为您也必须将 @current_year
传递给 calendar_path
。看起来这个条件总是将 2015 设置为当前年份。
@current_year = params[:year].blank? ? Date.today.year : params[:year].to_i
因为它适用于 12 月和 1 月,原因是您在月份为 0 or 13
这应该有效
<%= link_to '< Previous', calendar_path(@calendar, month: @current_month - 1, year: @current_year) %>
<%= link_to 'Next >', calendar_path(@calendar, month: @current_month + 1, year: @current_year) %>