How to control respond_to based on variable in Rails controller?
- by smotchkkiss
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