I have the following models:
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
In CanCan I am trying to define the abilities on a specific custom Responses action via attributes in the polymorphic association. The action looks like this:
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])
undivided_millisecond_epoch_time_in_integer = params[:after]
undivided_millisecond_epoch_time_in_decimal = (undivided_millisecond_epoch_time_in_integer).to_d
divided_millisecond_epoch_time_in_decimal = (undivided_millisecond_epoch_time_in_decimal / 1000000).to_d
@responses = @responseable.responses.where("created_at > ?", Time.at(divided_millisecond_epoch_time_in_decimal))
end
...
This action is run via a javascript function that polls for new responses every 5 seconds. However, when this runs I get the following errors in the log:
A NameError occurred in responses#polling:
uninitialized constant Responseable
activesupport (3.2.8) lib/active_support/inflector/methods.rb:230:in `block in constantize'
Any idea the right way to define these types of abilities?
You made quite a mess in that controller, try to follow REST (or CRUD for that matter).
Anyway, Cancan wont be able to detect which resource you are trying to authorize. Try something like this: