在 Rails 中,使用 Cancancan/Will_Paginate 如何只显示用户可以阅读的导航链接?
In Rails, using Cancancan/Will_Paginate how can I only show navigation links that user's can read?
我同时使用 cancancan
和 will_paginate
。某些用户能够阅读由以下块定义的某些报告。
Ability.rb
can :read, Report do |report|
if report.is_pro?
if (8..12).include? Time.now.month
(report.created_at.year != Time.now.year) || (report.author_id == user.id)
elsif (1..4).include? Time.now.month
(report.created_at.year < Time.now.year-1) || (report.author_id == user.id)
else
true
end
end
end
但是,当在如下给定页面上使用分页 links 时,限制起作用,用户只能看到能够看到的报告,但是 will_paginate links 的显示就好像所有报告都可见一样。
users_controller.html.erb
@reports = Report.all.paginate(page: params[:page], per_page: 15)
report_activity.html.erb
<% @reports.each do |report| %>
<% if can? :read, report %>
<% render 'user_report', report: report %>
<% end %>
<% end %>
<%= will_paginate @reports %>
我确实找到了一个 link,它提到在我的 ActiveQuery 中使用方法 accessible_by(current_ability)
。但这意味着我需要将上面显示的 Cancancan
能力块重写为散列。我不确定如何将我当前的逻辑作为哈希来处理。
所以我最终做了以下事情。希望这对其他人有帮助,但基本上我认为这只是重新考虑应该如何设置...
if (8..12).include?(Time.now.month) # Is the current month in Aug thru Dec?
# Allowed to read reports that are before August of the current year
can :read, Report, "((year(created_at) <> #{Time.now.year}) AND (month(created_at) >= 8)) OR reports.author_id = #{user.id}"
elsif (1..4).include?(Time.now.month) # Is the current month in Jan thru April?
# Allowed to read reports that are before August of the previous year
can :read, Report, "(year(created_at) < #{Time.now.year-1}) AND (month(created_at) <= 7) OR reports.author_id = #{user.id}"
else # or is it in May to July... wdgaf!
can :read, Report, :all
end
我同时使用 cancancan
和 will_paginate
。某些用户能够阅读由以下块定义的某些报告。
Ability.rb
can :read, Report do |report|
if report.is_pro?
if (8..12).include? Time.now.month
(report.created_at.year != Time.now.year) || (report.author_id == user.id)
elsif (1..4).include? Time.now.month
(report.created_at.year < Time.now.year-1) || (report.author_id == user.id)
else
true
end
end
end
但是,当在如下给定页面上使用分页 links 时,限制起作用,用户只能看到能够看到的报告,但是 will_paginate links 的显示就好像所有报告都可见一样。
users_controller.html.erb
@reports = Report.all.paginate(page: params[:page], per_page: 15)
report_activity.html.erb
<% @reports.each do |report| %>
<% if can? :read, report %>
<% render 'user_report', report: report %>
<% end %>
<% end %>
<%= will_paginate @reports %>
我确实找到了一个 link,它提到在我的 ActiveQuery 中使用方法 accessible_by(current_ability)
。但这意味着我需要将上面显示的 Cancancan
能力块重写为散列。我不确定如何将我当前的逻辑作为哈希来处理。
所以我最终做了以下事情。希望这对其他人有帮助,但基本上我认为这只是重新考虑应该如何设置...
if (8..12).include?(Time.now.month) # Is the current month in Aug thru Dec?
# Allowed to read reports that are before August of the current year
can :read, Report, "((year(created_at) <> #{Time.now.year}) AND (month(created_at) >= 8)) OR reports.author_id = #{user.id}"
elsif (1..4).include?(Time.now.month) # Is the current month in Jan thru April?
# Allowed to read reports that are before August of the previous year
can :read, Report, "(year(created_at) < #{Time.now.year-1}) AND (month(created_at) <= 7) OR reports.author_id = #{user.id}"
else # or is it in May to July... wdgaf!
can :read, Report, :all
end