Undefined method `accessible_by' for Model:Class

642 views
Skip to first unread message

Eugen Romas

unread,
Mar 25, 2014, 4:14:43 AM3/25/14
to canc...@googlegroups.com
Hello!

@models = Model.accessible_by(current_ability) gives me:
"NoMethodError at /models".

What am I doing wrong?

I am on Linux 3.2.0-60-generic #91-Ubuntu with ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-linux] , Rails 4.0.3 and 'mongoid', :github=>"mongoid/mongoid".

This is the Gemfile:
source 'https://rubygems.org'
gem 'rails', '4.0.3'
gem 'sass-rails', '~> 4.0.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'jquery-ui-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 1.2'
gem 'bootstrap-sass'
gem 'cancancan', '~> 1.7'
gem 'country_select'
gem 'devise'
gem 'mongoid', :github=>"mongoid/mongoid"
gem 'figaro'
gem 'rolify'
gem 'simple_form'
gem "kaminari"
group :development do
gem 'better_errors'
gem 'binding_of_caller', :platforms=>[:mri_19, :mri_20, :rbx]
gem 'quiet_assets'
gem 'rails_layout'
end
group :production do
gem 'unicorn'
end
group :doc do
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', require: false
end
gem 'debugger', group: [:development, :test]

Here are the two Controllers:

class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
rescue_from CanCan::AccessDenied do |exception|
redirect_to root_path, :alert => exception.message
end

before_filter :set_locale
before_filter :authenticate_user!
before_filter :set_current_user

def set_locale
unless request.env['HTTP_ACCEPT_LANGUAGE'].blank?
logger.debug "* Accept-Language: #{request.env['HTTP_ACCEPT_LANGUAGE']}"
I18n.locale = extract_locale_from_accept_language_header
# I18n.locale = 'en'
logger.debug "* Locale set to '#{I18n.locale}'"
end
end

private


def extract_locale_from_accept_language_header
request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first
end

protected

def permission_denied
flash[:error] = t('controllers.application.permission_denied.flash.error.forbidden', :default => "Sorry, you are not allowed to access that page.")
redirect_to root_url
end

def set_current_user
@current_user = current_user
end

def t(key, opts = {})
prefix = "controllers."+ params[:controller] + "." + params[:action]
if key.chr == "."
begin
path = prefix + controller_path.split("/").join(".") + key
I18n.t(path, :raise => I18n::MissingTranslationData)
rescue I18n::MissingTranslationData
path = prefix + ".flash" + key
end
else
path = key
end
I18n.t(path, opts)
end

end


class PeopleController < ApplicationController
before_filter :authenticate_user!
load_and_authorize_resource
skip_authorize_resource :only => :new

def index
sel = {}
unless params[:search].blank?
sel.merge!("full_name"=>/#{params[:search][:q1]}/i) unless params[:search][:q1].blank?
unless params[:search][:q2].blank?
user_ids = User.where("name"=>/#{params[:search][:q2]}/i).only(:_id).map(&:_id)
if user_ids
sel.merge!("user_id"=> {:$in=>user_ids})
end
end
unless params[:search][:q3].blank?
user_ids = User.where("email"=>/#{params[:search][:q3]}/i).only(:_id).map(&:_id)
if user_ids
sel.merge!("user_id"=> {:$in=>user_ids})
end
end
end
@people = Person.accessible_by(current_ability).search(sel).page(params[:page]).per(10)
end

def show
end

def new
@person.address = Address.new
@person.contact = Contact.new
if params[:user_id]
@person.user = @current_user
end
@cusers = []
User.accessible_by(current_ability).each{|u|@cusers << [u.email, u.id] if u.person.blank?}
end

def edit
end

def create
if @person.save
redirect_to @person, notice: t('controllers.people.created', :default => 'Person was successfully created.')
else
render action: "new"
end
end

def update
if @person.update_attributes(resource_params)
redirect_to @person, notice: t('controllers.people.updated', :default => 'Person was successfully updated.')
else
render action: "edit"
end

end

def destroy
if @person.destroy
redirect_to people_url, notice: t('controllers.people.destroyed', :default => 'Person was successfully destroyed.')
end
end

private

def resource_params
params.require(:person).permit(:full_name, :salutation, :address_attributes, :contact_attributes, :user_id)
end

end

This works fine with rails 3.2.17 and CanCan, without strong parameters.

Need help.

eu...@romas.de

brite

unread,
Mar 25, 2014, 3:47:05 PM3/25/14
to canc...@googlegroups.com, eu...@romas.de
I'm a little confused... at first glance, this should all be fine. Where are you entering:

@models = Model.accessible_by(current_ability)

The command line or in the controller somewhere? What action?

Eugen Romas

unread,
Mar 25, 2014, 5:19:12 PM3/25/14
to canc...@googlegroups.com
Sorry, I forgot to update the line:

@models = Model.accessible_by(current_ability)

which was an example only, to:

@people = Person.accessible_by(current_ability).search(sel).page(params[:page]).per(10)

If I do:

@people = Person.all.search(sel).page(params[:page]).per(10)

it workes.
In the mean time I debugged and didn't find the address of the module accessible_by in the PATH.

If it makes sense I can post the debugging output. Please feel free to ask for further information.

I tried it with rails 3.2.17 and CanCanCan, without strong parameters, which workes fine.

I think there is a bug with rails 4.0.3.

eu...@romas.de

----- Ursprüngliche Mail -----
Von: "brite" <br...@bryanrite.com>
An: canc...@googlegroups.com
CC: eu...@romas.de
Gesendet: Dienstag, 25. März 2014 20:47:05
Betreff: Re: Undefined method `accessible_by' for Model:Class
> eu...@romas.de <javascript:>
>

brite

unread,
Mar 27, 2014, 12:31:46 PM3/27/14
to canc...@googlegroups.com, eu...@romas.de
Hi Eugen,

I've tried to replicate this but I haven't been successful... everything works fine for me on 4.0.3...

Would you be able to create minimal rails project that demostrates this behaviour and share it on github? I can't duplicate it myself so I'm kind of stuck.


On Tuesday, 25 March 2014 01:14:43 UTC-7, Eugen Romas wrote:

Eugen Romas

unread,
Mar 28, 2014, 11:21:40 AM3/28/14
to canc...@googlegroups.com
Hi Bryan,
I got it. The cause of the error was the bad order of the gems in "Gemfile".
With the new reordered "Gemfile":

...
gem 'bootstrap-sass'
gem 'mongoid', :github=>"mongoid/mongoid"
gem 'devise'
gem 'cancancan', '~> 1.7'
gem 'country_select'
gem 'figaro'
gem 'rolify'
gem 'simple_form'
gem "kaminari"
group :development do
...

now it works fine.

Many thanks for helping me getting the right idea for the solution of this problem.

eu...@romas.de

----- Ursprüngliche Mail -----
Von: "brite" <br...@bryanrite.com>
An: canc...@googlegroups.com
CC: eu...@romas.de
Gesendet: Donnerstag, 27. März 2014 17:31:46
Betreff: Re: Undefined method `accessible_by' for Model:Class

> eu...@romas.de <javascript:>
>
Reply all
Reply to author
Forward
0 new messages