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

  • SEARCH
  • Home
  • 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 6341689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:05:16+00:00 2026-05-24T20:05:16+00:00

right now my app has a Pages model. Anyone with the url, /pages/3 can

  • 0

right now my app has a Pages model. Anyone with the url, /pages/3 can view a page. I want to make makes have the option of public or private.

  • If public anyone with the URL can view.

  • If the page if private, only users that enter a password should be able to view the page.

Right now the page is rendered with the Page#Show controller. What’s the right way to go about handling this so that when a user tries to access a private page they first need to enter a password correctly and then they can view the page? How would I structure this in the controller?

Thanks

  • 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-05-24T20:05:17+00:00Added an answer on May 24, 2026 at 8:05 pm

    Since you are using cancan:

    def show
      @page = Page.find(params[:id])
      authorize! :read, @page
    end
    

    This will raise a CanCan::AccessDenied error (if the user isn’t logged in or isn’t authorized), which can be caught like so (docs):

    class ApplicationController < ActionController::Base
      rescue_from CanCan::AccessDenied do |exception|
        # Save the requested path in the user's session
        session[:return_to] = request.fullpath
        # Send the user to the sign in page
        redirect_to sign_in_path, :alert => exception.message
      end
    end
    

    Depending on your authentication system, on a successful sign-in, in your sessions controller you can:

    redirect_to session[:return_to] || your_default_after_sign_in_path
    # Clear the :return_to value
    session[:return_to] = nil
    

    I may be missing some details, but this is the gist of it. Best of luck.

    EDIT:
    I should attribute the friendly forwarding part of my answer to Michael Hartl and his book.

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

Sidebar

Related Questions

I have a model that has one column right now, description , that populates
I am working on an app right now where I have Individuals, Athletes and
I've got an app that's in invite-only beta right now. Problem is, I can't
I have my core web-app code in the header right now in a tag
I have a app that plays video. Right now it exits when the vedio
I have an app that has several different pages that use Sencha Touch. Ocassionally
Background info I have an app where users can make microsites ala http://myname.the_app.com .
I have a web page with some jQuery code. Right now I am hardcoding
Has anyone encountered this problem with Windows Phone 7.1? I have a simple page
I am developing an app right now that reads in data from a windows

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.