cancan

How to do pagination with cancan?

萝らか妹 提交于 2019-12-05 07:13:52
I'm looking to do pagination with cancan however it's not obvious how to integrate this with gems such as will_paginate. Ideally cancan's load_resource will delegate to will_paginate and add extra conditions. For example in cancan I've declared guest users can :read, Post, :published => true and this is handled automatically by load_resource. However I'd then like to have will_paginate page through all these results. Any ideas. Regards Brad This is simple to do with kaminari https://github.com/amatsuda/kaminari in my PostsController I just do before_filter :load_by_pagination, :only => :index

Rails: Using CanCan to define multiple roles depending on instances of single Model?

杀马特。学长 韩版系。学妹 提交于 2019-12-05 05:24:29
I'm currently stuck on how to separate roles for CanCan depending on each condition that we want. In our application, there are many categories (such as math, english, history, etc.) and within each are many courses. Each user can have many different roles on each category. For example, John can be a "reader" for math, which means he can read all the courses that are in math. John can also be a "writer" for english, which means he can read all the courses in english, create a course within category english, and edit/delete only the courses he created within english. If these were the only

Cancan Thinking Sphinx current_ability Questions

倖福魔咒の 提交于 2019-12-05 04:48:29
问题 trying to get cancan working with thinking sphinx but running into some issues. Before using sphinx, I had this in my companies view: @companies = Company.accessible_by(current_ability) That prevented my users from seeing anyone else's companies... After installing sphinx, I ended up with: @companies = Company.accessible_by(current_ability).search(params[:search], :include => :order, :match_mode => :extended ).paginate(:page => params[:page]) Which now displays all my companies and isn't

How do I use cancan to authorize an array of resources?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 23:02:06
问题 I have a non-restful controller that I am trying to use the cancan authorize! method to apply permissions to. I have a delete_multiple action that starts like so def delete_multiple @invoices = apparent_user.invoices.find(params[:invoice_ids]) I want to check that the user has permission to delete all of these invoices before proceeding. If I use authorize! :delete_multiple, @invoices permission is refused. My ability.rb includes the following if user.admin? can :manage, :all elsif user

Allow users to edit/destroy their own profiles only from the index

一曲冷凌霜 提交于 2019-12-04 22:43:27
How do I grant permission to edit/ destroy links on a by-user basis in the user index of rails? I'm using Rails3, Devise and CanCan to define role based abilities. I'd like the current user to be able to see and access a link to edit/delete their profile in the user index page. They should not be able to see or access these links for all other users. I've set up the following in the index view: <% if can? :update, @user %> <%= link_to 'Edit', edit_user_registration_path(@user) %> | <% end %> And in abilities.rb def initialize(user) can :update, User, :id => user.id if user.role? :super_admin

Setting up different User models and registration paths for Devise on Ruby on Rails

↘锁芯ラ 提交于 2019-12-04 19:55:47
I am very new to ruby and I have been really struggling with this for months. I searched extensively and tried what the answers said but still no luck. (I tried Multiple user models with Ruby On Rails and devise to have separate registration routes but one common login route but didnt work) I currently have a user.rb model and it is connected to devise and works fine. 1- On the sign-up page, I would like to have 3 buttons that would lead to separate registration forms (one each for business, manager and the already existing user). Do I set this up in routes.rb? 2- The forms will have different

Devise/CanCanCan - Allow Admin To Create New Users

故事扮演 提交于 2019-12-04 15:54:44
I'm using rails 4.0.2, devise and cancancan . I'm trying to allow an admin to create new users. Admin users are assigned with a boolean field in the users table. In ability.rb I have the following: can :manage, :all if user.admin? Following some of the advise in this question I created a new controller called AdminsController and it looks like so: class AdminsController < Devise::RegistrationsController def create build_resource(sign_up_params) if resource.save redirect_to admin_editors_path else clean_up_passwords resource respond_with resource end end def new build_resource({}) end end I've

Devise + CanCan just prevent other users from editing objects

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 14:38:39
问题 How would you prevent other users from editing a object, say a profile object that does - not - belong to themselves? Most online examples are complexes with multiple user roles, i haven't been able to get this working, must be simple though: def initialize(user) can :update, Profile do |profile| profile.try(:user) == current_user end end And inside my ProfilesController#edit authorize! :update, @profile 回答1: First question is, have you made your roles for the User ? app/models/user.rb class

mocking CanCan authorization while testing controllers with RSpec

不羁岁月 提交于 2019-12-04 14:11:57
Here is the Controller I want to test: class UsersController < ApplicationController load_and_authorize_resource def index @users = User.all respond_to do |format| format.html # index.html.erb format.json { render json: @users } end end def show @user = User.find(params[:id]) respond_to do |format| format.html # show.html.erb format.json { render json: @user } end end #other actions here end As you can see I use CanCan method load_and_authorize_resource so I've written a ControllerHelper for RSpec: # spec/support/controller_spec.rb module ControllerHelper def should_authorize(action, subject)

How do I properly test CanCan abilities with RSpec

孤街醉人 提交于 2019-12-04 14:07:19
问题 I am testing CanCan abilities for the first time and am stumped. I'm missing something...even if I return false/true inside of the can :invite_to block I am still not getting passing specs. Am I missing using the CanCan matchers? or stubs? or definiing abilities in CanCan? Anything I'm missing? ability.rb class Ability include CanCan::Ability def initialize(user) user ||= User.new can :invite_to, Network do |network| network.allows_invitations? && (user.admin? || user.can_send_invitations_for