Skip to content

Instantly share code, notes, and snippets.

@Sparkmasterflex
Created September 5, 2014 16:16
Show Gist options
  • Save Sparkmasterflex/566953a35a3dacdb96d5 to your computer and use it in GitHub Desktop.
Save Sparkmasterflex/566953a35a3dacdb96d5 to your computer and use it in GitHub Desktop.
Solution for the CanCan issue with Rails strong parameters. This uses a method in the ApplicationController and calling it at the top of create/update action
class ApplicationController < ActionController::Base
...omitted code...
def check_ability action, obj
raise CanCan::AccessDenied unless can?(action, obj)
end
end
class UsersController < ApplicationController
load_and_authorize_resource except: [:create, :update]
def create
check_ability :create, User
@user = User.new(user_params)
if @user.save
...omitted...
else
...omitted...
end
end
def update
@user = User.find(params[:id])
check_ability :update, @user
if @user.update_attributes(user_params)
...omitted...
else
...omitted...
end
end
private
def user_params
...omitted...
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment