【发布时间】:2017-04-10 10:47:20
【问题描述】:
我有 2 个表:posts 和 users(它们的关系是多对多的),User 有很多 favorite_posts(带有 FavoritePost 表(它由 user_id 和 post_id 组成)。
所以,我有一条路线:
获取'favorite_posts',到:'favorite_posts#index'
(用户/:user_id/favorite_posts)
以我的能力.rb:
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new
if user.new_record?
can :read, [Post]
else
can :read, [Post]
can :manage, [Post], owner_id: user.id
can :manage, [FavoritePost], user_id: user.id
end
end
end
在我的控制器中(favorite_posts_controller.rb):
class FavoritePostsController < ApplicationController
load_and_authorize_resource through: :current_user
def index
@favorite_posts = User.find(params[:user_id]).favorite_posts
end
所以,我需要通过ability.rb 阻止重定向到其他用户最喜欢的帖子的页面。我需要做什么?
【问题讨论】:
标签: ruby-on-rails ruby cancancan