Ruby on rails Rails 4:在“显示视图”中按自定义属性过滤对象
在我的Rails 4应用程序中,我有以下型号:Ruby on rails Rails 4:在“显示视图”中按自定义属性过滤对象,ruby-on-rails,date,ruby-on-rails-4,filter,navigation,Ruby On Rails,Date,Ruby On Rails 4,Filter,Navigation,在我的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 belong
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
在日历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) %>
(然后我有一个循环来显示相关的帖子)
上面的代码在今年运行得很好,也就是说,我可以从一个月导航到另一个月,每个月我都会得到正确的帖子
但是,当我尝试导航到上一年(多次点击“- 月份顺序为2015年1月至2014年12月至2015年11月(或2015年12月至2016年1月至2015年2月),这意味着@current_year不再正确
- 因此(这实际上是查询工作正常的一个迹象),我再次收到相同的帖子,因为我没有导航到2014年11月,而是导航到2015年11月,因此显示了2015年11月的帖子(2016年2月与2015年2月的问题相同)
你知道我的代码出了什么问题吗?我想你也得把
@当前年份
传递到日历路径
。看起来这种情况总是将2015年定为当前年份
@current_year = params[:year].blank? ? Date.today.year : params[:year].to_i
它适用于12月和1月的原因是,当月份为0或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) %>
非常感谢您。您对问题的识别和解决方法都是完全正确的。现在一切正常。
<%= 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) %>