Can can with devise, admin and user
NickName:Dean Gergorić Ask DateTime:2015-03-27T23:35:36

Can can with devise, admin and user

I use cancan and devise, I can update delete and show but I can't create profile. why I can't create new profile ("ActiveModel::ForbiddenAttributesError")

class Ability
  include CanCan::Ability

  def initialize(user)
    if user.is_a?(Admin)
      can :manage, :all
    elsif user.is_a?(User)

        can :read, Profile do |profile|
        profile.try(:user) == user
        end
        can :update, Profile do |profile|
        profile.try(:user) == user
        end
        can :destroy, Profile do |profile|
        profile.try(:user) == user
        end
        can :create, Profile do |profile|
        profile.try(:user) == user 
    else
      can :read, :all
    end
  end
end

Copyright Notice:Content Author:「Dean Gergorić」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/29304528/can-can-with-devise-admin-and-user

Answers
Dean Gergorić 2015-03-27T15:56:11

class ProfilesController < ApplicationController\n before_action :set_profile, only: [:show, :edit, :update, :destroy]\n load_and_authorize_resource\n\n\n # GET /profiles\n # GET /profiles.json\n def index\n user = User.find(params[:user_id])\n @profiles = user.profiles\n\n respond_to do |format|\n format.html\n format.xml {render :xml => @profiles}\n end\n end\n\n # GET /profiles/1\n # GET /profiles/1.json\n def show\n user = User.find(params[:user_id])\n @profiles = user.profiles.find(params[:id])\n\n respond_to do |format|\n format.html\n format.xml {render :xml => @profile}\n end\n end\n\n # GET /profiles/new\n def new\n user = User.find(params[:user_id])\n @profile = user.profiles.build\n\n respond_to do |format|\n format.html\n format.xml {render :xml => @profile}\n end\n end\n\n # GET /profiles/1/edit\n def edit\n user = User.find(params[:user_id])\n @profiles = user.profiles.find(params[:id])\n end\n\n # POST /profiles\n # POST /profiles.json\n def create\n user = User.find(params[:user_id])\n @profile = user.profiles.create(profile_params)\n\n respond_to do |format|\n if @profile.save\n format.html { redirect_to user_profiles_url, notice: 'Profile was successfully created.' }\n format.json { render action: 'show', status: :created, location: @profile }\n else\n format.html { render action: 'new' }\n format.json { render json: @profile.errors, status: :unprocessable_entity }\n end\n end\n end\n\n # PATCH/PUT /profiles/1\n # PATCH/PUT /profiles/1.json\n def update\n user = User.find(params[:user_id])\n @profiles = user.profiles.find(params[:id])\n\n respond_to do |format|\n if @profile.update(profile_params)\n format.html { redirect_to user_profile_url, notice: 'Profile was successfully updated.' }\n format.json { head :no_content }\n else\n format.html { render action: 'edit' }\n format.json { render json: @profile.errors, status: :unprocessable_entity }\n end\n end\n end\n\n # DELETE /profiles/1\n # DELETE /profiles/1.json\n def destroy\n user = User.find(params[:user_id])\n @profiles = user.profiles.find(params[:id])\n\n @profile.destroy\n respond_to do |format|\n format.html { redirect_to job_hunters_path }\n format.json { head :no_content }\n end\n end\n\n private\n # Use callbacks to share common setup or constraints between actions.\n def set_profile\n @profile = Profile.find(params[:id])\n end\n\n # Never trust parameters from the scary internet, only allow the white list through.\n def profile_params\n params.require(:profile).permit(:full_name, :phone_number, :email, :position, :years_of_experiance, :cover_letter, :resume, :reference)\n end\nend\n",


More about “Can can with devise, admin and user” related questions

Can can with devise, admin and user

I use cancan and devise, I can update delete and show but I can't create profile. why I can't create new profile ("ActiveModel::ForbiddenAttributesError") class Ability include CanCan::Ability ...

Show Detail

I can`t create admin user on devise

I'm using Rails 3.2.13 and Devise. Devise is working fine but I cant create“admin”` user. I followed this https://github.com/plataformatec/devise/wiki/How-To:-Add-an-Admin-Role#option-2---adding-an-

Show Detail

How can I authenticate a user with admin attribute for rails_admin with devise

I am fairly new to ruby on rails. I habe set up a mysql database, connected it with a rails app, installed the devise gem and rails_admin gem. So far so good, everything works as it should. Now I a...

Show Detail

rails_admin with devise and single admin user

I'm using Rails 5, rails_admin and devise with the standard devise user model setup: config.authenticate_with do warden.authenticate! scope: :user end config.current_user_method(&amp;:

Show Detail

Add User from Admin with Devise in ROR

I m new to ROR, I want to add Sign-in and Sign-up in my ROR application to add users with role "Admin" and "Vendor" and for this i m using Devise, which created Sign-In and Sign-up and are working ...

Show Detail

Admin user administration with Devise

I am trying out Devise for the first time. One of the things that I wanted to do is provide an interface for Admin users to create, find and edit users. Here's where I may have gone wrong. I crea...

Show Detail

Active Admin with devise multiple user roles

I was wondering how can I create a smooth flow of user roles with Active Admin and devise. I need to create 3 roles: user, moderator, administrator I saw this post here: https://github.com/heartcombo/

Show Detail

Rails and Devise: Can't use admin and user views

I generated two models: Admin and User for my application, and I also generated views for each of them: rails generate devise user rails generate devise admin rails g devise:views admin rails g

Show Detail

Admin Management User devise routing ruby on rails example

Lets say I have 3 devise models (admin, management,user) and I have article model I want to create url (routing) in this way: for devise models localhost/admin localhost/management localhost/u...

Show Detail

Creating an admin user in Devise on Rails beta 3

Ok, I'm probably going to feel quite dumb when someone answers this one with a simple thing that I'm missing but... here goes: I've got a brand new app on rails 3 beta and I'm using devise for the

Show Detail