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