cancan

How to do pagination with cancan?

不羁岁月 提交于 2019-12-22 06:33:15
问题 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 回答1: This is simple to do with kaminari https:/

Rspec controller error expecting <“index”> but rendering with <“”>

三世轮回 提交于 2019-12-22 04:11:04
问题 New to testing, I'm struggling to get some controller tests to pass. The following controller test throws the error: expecting <"index"> but rendering with <""> I have the following in one of my controller specs: require 'spec_helper' describe NasController do render_views login_user describe "GET #nas" do it "populates an array of devices" do @location = FactoryGirl.create(:location) @location.users << @current_user @nas = FactoryGirl.create(:nas, location_id: @location.id ) get :index

Rspec controller error expecting <“index”> but rendering with <“”>

和自甴很熟 提交于 2019-12-22 04:10:12
问题 New to testing, I'm struggling to get some controller tests to pass. The following controller test throws the error: expecting <"index"> but rendering with <""> I have the following in one of my controller specs: require 'spec_helper' describe NasController do render_views login_user describe "GET #nas" do it "populates an array of devices" do @location = FactoryGirl.create(:location) @location.users << @current_user @nas = FactoryGirl.create(:nas, location_id: @location.id ) get :index

Getting Cancan's load_and_authorize_resource working within a custom create action

旧巷老猫 提交于 2019-12-21 19:43:15
问题 Trying to set up Cancan within an app of mine and having trouble with my PostsController . In a nutshell, when a Post is created I'd like it associated with the current_user so my create action looks something like this: class PostsController < ApplicationController before_filter :login_required, :except => [:index, :show] load_and_authorize_resource ... def create # @post = Post.new(params[:post]) # <-- covered by load_and_authorize_resource @user = current_user @post = @user.posts.create

What is the best way to bypass devise authorization for a specific record marked public

我是研究僧i 提交于 2019-12-21 03:57:06
问题 I'm using devise and cancan in a Rails 3.2 project. I have an event model with a boolean flag public . If the event is marked as public => true then I want anybody, signed in or not to be able to access the record with GET /events/:id If it is marked as public => false then a series of cancan abilities will decide authorization and access to the above resource. What is the best pattern for achieving this? 回答1: You can do that by skip the authenticate_user! in case of you have this args skip

How to access 'can?' method from within cell?

拟墨画扇 提交于 2019-12-21 02:17:30
问题 I'm using cancan and cells gems in my ruby-on-rails project. How to access can? method from within cell? Thanks. 回答1: I've had to do exactly this. Try class MyCell < Cell::Rails include CanCan::ControllerAdditions end If you're also using Devise, I had to do this: class MyCell < Cell::Rails include CanCan::ControllerAdditions include Devise::Controllers::Helpers Devise::Controllers::Helpers.define_helpers(Devise::Mapping.new(:user, {})) end #define_helpers will add helper methods such as

How to access 'can?' method from within cell?

半城伤御伤魂 提交于 2019-12-21 02:17:10
问题 I'm using cancan and cells gems in my ruby-on-rails project. How to access can? method from within cell? Thanks. 回答1: I've had to do exactly this. Try class MyCell < Cell::Rails include CanCan::ControllerAdditions end If you're also using Devise, I had to do this: class MyCell < Cell::Rails include CanCan::ControllerAdditions include Devise::Controllers::Helpers Devise::Controllers::Helpers.define_helpers(Devise::Mapping.new(:user, {})) end #define_helpers will add helper methods such as

Why is this rspec request spec not updating the model?

偶尔善良 提交于 2019-12-20 21:00:52
问题 I have a requests spec for interactions with the User model. I want to make sure that Users with the Admin role can create/edit/destroy Users. I'm having a problem right now where the Edit action does not update the user. Everything works properly when I manually go through the actions on the site itself, but the tests fail to update the user. Here's my spec: it 'edits a user' do @user = FactoryGirl.create(:user) visit new_user_session_path unless current_path == new_user_session_path fill_in

Authorizing Namespaced and Nested controllers using CanCan

左心房为你撑大大i 提交于 2019-12-19 08:02:10
问题 I having quite a bit of troubling getting cancan to authorize my new routes setup below: namespace :api do namespace :v1 do resources :users do resources :user_songs resources :friendships resources :plays resources :likes resources :songs I have followed what was posted here https://github.com/ryanb/cancan/wiki/Nested-Resources and tested it with the likes controller by putting this above: class Api::V1::LikesController < Api::V1::BaseController load_and_authorize_resource :user load_and

cancan abilities in separate file

折月煮酒 提交于 2019-12-19 04:15:15
问题 Is it possible to define abilities in separate file and include them in ability.rb file inside initialize method ? belowed code returns: tried and got: undefined method 'can' ability.rb def initialize(user) include MyExtension::Something::CustomAbilities ... end lib/my_extension/something.rb module MyExtension::Something module CustomAbilities can :do_it, Project do |project| check_something_here and return true or false... end end end perfect solution, if possible, would be to extend class