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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:19:34+00:00 2026-05-14T03:19:34+00:00

The dilemma I’m using a before_filter in my controller that restricts access to admins.

  • 0

The dilemma

I’m using a before_filter in my controller that restricts access to admins. However, I want to allow access public access to some methods based on request format. See the index method to understand what I’m talking about.

application_controller.rb

class ApplicationController < ActionController::Base

  # ...

  def current_user
    @current_user ||= User.find_by_id(session[:user])
  end

  def superuser_required
    redirect_to login_path unless current_user.superuser?
  end


end

items_controller.rb

class ItemsController < ApplicationController

  before_filter :superuser_required
  layout 'superuser'

  def index
    @items = Item.all
    respond_to do |format|
      format.html
      format.js # I want public to have access to this
    end
  end

  def show
    @item = Item.find(params[:id])
    respond_to do |format|
      format.html
    end
  end

  def new
    @item = Item.new
    respond_to do |format|
      format.html
    end
  end

  # remaining controller methods
  # ...

end
  • 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-14T03:19:35+00:00Added an answer on May 14, 2026 at 3:19 am

    Way easier than I expected

    2 days later

    class FoosController < ActiveRecord::Base
    
      # use :except to open `show` action for public js access
      before_filter :superuser_required, :except => 'index'
    
      # before_filter does not apply here
      def index
        @foos = Foo.all
    
        respond_to do |format|
    
          # restrict behavior on html access for superusers
          format.html do
            superuser_required  # functions same as before_filter
          end
    
          # unrestricted on js access for public but only shows foo.id and foo.name
          format.js do
            render :text => @foo.to_json(:only => [:id, :name])
          end
    
        end
      end
    
      # restricted to superuser per before_filter
      def new
        @foo = Foo.new
        respond_to do |format|
          format.html
        end
      end
    
      # restricted to superuser per before_filter
      def show
        @foo = Foo.find(params[:id])
        respond_to do |format|
          format.html
        end
      end
    
    end
    

    Either I totally missed something when I was learning about respond_to or my original question was completely incoherent. I just read it again, though, and it still seems like this only (and most) appropriate way to address my issue.

    Surprisingly, I couldn’t really find any examples of this kind of behavior on the web either. Oh well. Now I know more about respond_to, right?

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

Sidebar

Related Questions

So here is my dilemma, I have a navigation controller that controls three views.
I have some dilemma about controller usage. Like usualy, router (dispatcher or front controller)
Here is my dilemma. I have a collection of entities that I want to
I have a little dilemma that maybe you can help me sort out. I've
We have a dilemma; developing desktop application using Matisse or Jigloo.. Matisse has this
I have a dilemma that I've encountered before. What's the best in terms of
I ran into this dilemma when working on an ASP.net web application using Web
I'm facing a dilemma (!). In a first scenario, I implemented a solution that
My dilemma is that I need to compile a large amount of projects and
I'm having a dilemma. I have a field hashedX that is a hashed/salted value

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.