How to control respond_to based on variable in Rails controller?

Posted by smotchkkiss on Stack Overflow See other posts from Stack Overflow or by smotchkkiss
Published on 2010-03-21T17:21:44Z Indexed on 2010/03/21 17:31 UTC
Read the original article Hit count: 394

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.

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
  # ...

  def superuser_required
    redirect_to login_path unless current_user.superuser?
  end

end

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about rails