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 9220087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:16:00+00:00 2026-06-18T03:16:00+00:00

Post: hidden: boolean I want the logged in user could see all the posts,

  • 0
Post: hidden: boolean

I want the logged in user could see all the posts, and the non-logged-in user only have access to posts whose hidden fields are false.
So I write like this in cancan’s Ability Model:

if user_signed_in?
    can :read, Post
else 
    can :read, Post, :hidden => false
end

but accessing the helper user_signed_in is not allowed in Model.
As stated in this question: Rails 3 devise, current_user is not accessible in a Model ?. While we could using some tricks to access the helper, its not proper to do that

So, how could I authorize the not-logged-in user properly? Or just use “Include” to use this helper ??

Or should I put this in authentication part? but how?

  • 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-18T03:16:01+00:00Added an answer on June 18, 2026 at 3:16 am

    All you need to do is this:

    def initialize(user)
      user ||= User.new # guest user (not logged in)
    
      can :read, Post, :hidden => false
    
      if User.exists?(user)
        can :read, Post
      end
    end
    

    With devise, the current_user helper method returns the current user when logged in but returns nil when not logged in. It is available in the controllers and views. By default, CanCan does all authorization checks against the return of the current_user method.

    Now whenever the can? method is called from a view or a controller, the return of current_user will be passed to a new instance of Ability as the local variable user.

    To check if the user was logged in, I choose to useUser.exists?(). It’s a class method for ActiveRecord::Base that will check if that user object is persisted in the database. Any other way will work just as well though. For instance, this would work just as well or better:

    if user.encrypted_password
      can :read, Post
    end
    

    This will check if the default devise password field exists for the user instance. If you haven’t done anything too crazy, this will only return a value if the user is logged in. If the second option works for your situation, it may be superior because you won’t even have to query the database.

    Semi-relevant tip, check out a role handling gem like Rolify. It is great when used in conjunction with CanCan.

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

Sidebar

Related Questions

Post :belongs_to :user User :has_many :posts In my signup workflow they draft a Post
I have a post & request. I need when user on post page click
I have my wordpress post content hidden using a jquery toggle effect. The problem
I have list of posts that any user can edit. When I enter edit
I recently read the following overflow post: Hidden Features of C# One of the
I am trying to get post a form to a hidden, dynamically inserted iframe,
I was reading through this thread: Hidden Features of JavaScript? and found this post:
Post-release, I have made one small change to one form in our development site
Similar Post: Hidden Features of Visual Studio (2005-2008)? What are some shortcut keys you
I have a hidden input field that I fill with some arbitrary value using

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.