我有以下型号:
class Post < ActiveRecord::Base
has_many :responses, as: :responseable, dependent: :destroy
end
class Call < ActiveRecord::Base
has_many :responses, as: :responseable, dependent: :destroy
end
class Meeting < ActiveRecord::Base
has_many :responses, as: :responseable, dependent: :destroy
end
class Response < ActiveRecord::Base
belongs_to :responseable, polymorphic: true # Tested
end
在 CanCan 中,我试图通过多态关联中的属性来定义特定自定义响应操作的能力。动作如下所示:
class ResponsesController < ApplicationController
before_filter :authenticate_user!
load_and_authorize_resource
respond_to :html, :xml, :js, :json, :pdf
# GET /responses/polling
# GET /responses/polling.json
def polling
responseable_type = params[:responseable_type]
klass = [Post, Call, Meeting].detect { |c| responseable_type}
@responseable = klass.find(params[:responseable_id])
@responses = @responseable.responses.where("created_at > ?", Time.at(params[:after]))
end
...
我的能力文件如下所示:
...
can :polling, Response, :responseable_type = "Post", :responseable => { :user_expert_private => false, :countries => { :id => user.country_ids} }
...
此操作通过一个 JavaScript 函数运行,该函数每 5 秒轮询一次新响应。但是,当它运行时,我在日志中收到以下错误:
A NameError occurred in responses#polling:
uninitialized constant Responseable
activesupport (3.2.8) lib/active_support/inflector/methods.rb:230:in `block in constantize'
知道从多态关系中定义属性能力的正确方法吗?
基本上,我不希望用户能够查看/创建响应,除非他们can? show:
是父对象。