Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7991537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:19:08+00:00 2026-06-04T13:19:08+00:00

I have users class User < ActiveRecord::Base devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable attr_accessible

  • 0

I have users

class User < ActiveRecord::Base
  devise :database_authenticatable,
         :recoverable, :rememberable, :trackable, :validatable


  attr_accessible :email, :password, :password_confirmation,
                  :remember_me, :site_id, :role_name

  belongs_to :site

end

sites

class Site < ActiveRecord::Base
  has_many :users
  has_one :front_page_campaign
end

and front_page_campaigns

class FrontPageCampaign < ActiveRecord::Base
  belongs_to :site
end

I’m using cancan to restrict access, so users can only manage front_page_campaigns for their own site:

class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= User.new # guest user (not logged in)

    case user.role_name

    when "super_admin"
      # can do everything
      can :manage, :all

    when "editor"
      # can edit content for their site
      can [:create, :read, :update], FrontPageCampaign, site_id: user.site_id

    end
  end
end

This works perfectly for users with role_name super_admin and also for editor on show and edit on front_page_campaigns. But when an editor tries to create a new front_page_campaign, I get a cancan forbidden notice

You are not authorized to access this page.

The standard form offers a dropdown box of all sites, and I guess I need to restrict this to just the user’s own site. How would I go about doing this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T13:19:10+00:00Added an answer on June 4, 2026 at 1:19 pm

    Your authorization issue is solved by adding:

    can :new, FrontPageCampaign
    

    to the editor section of cancan ability init.
    To set site_id on the new and create object you can set up a before_filter:

    # FrontPageCampaignsController
    before_filter :set_site_id, :only => [:new, :create]
    
    protected
    
    def set_site_id
      @resource.site_id = current_user.site_id if current_user && current_user.role_name == 'editor'
    end
    

    you gotta make sure this fires after the resource is created but before can can authorization.

    In your form (if you use the same for superadmin and editor) make the site dropdown selection readonly or hidden if current_user.role_name == 'editor'.

    Note that if someone tampers with the form and sends an alien site_id as editor, it will be corrected by the before filter, which is not nice. If you take if out and have :only => :new then they will get authorization error by cancan. If you are super pedant, you should instead get a valid response with validation error. You can achieve this by 1) applying the before_filter only to new and 2) say in ability init

    when "editor"
      # can edit content for their site
      can [:read, :update], FrontPageCampaign, site_id: user.site_id
      can [:create, :new], FrontPageCampaign
    end
    

    and 3) add site owner checking to model validation. This is my preferred way, keeping authorization errors for illegal access of existing resources.

    hope this answers your question

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have model User class User < ActiveRecord::Base has_and_belongs_to_many :roles attr_accessible :login, :email, :password,
I have 2 models Users(devise gem) and Profiles where class User < ActiveRecord::Base has_one
I have a very simple issue: User model: class User < ActiveRecord::Base devise :database_authenticatable,
First here's my current setup: models/user.rb class User < ActiveRecord::Base devise :database_authenticatable, :registerable, :recoverable,
I have a table called Users ( class User < ActiveRecord::Base ) and a
I have two models: User and Teacher : class User < ActiveRecord::Base attr_accessor :password
I have two models: User (email:string) Profile (name:string) class User < ActiveRecord::Base has_one :profile
Here is a Ruby ActiveRecord class: class User < ActiveRecord::Base has_many :user_tokens devise :trackable,
I have 3 models sites, user_favorites and users. Relevant relationships: class Site < ActiveRecord::Base
I have a user and nested profile class as follows: class User < ActiveRecord::Base

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.