I have a controller which via html shows the user profile, via JSON shows a user hover card w profile info.
class UsersController < ApplicationController
def show
@user = User.where(:id => params[:id]).first
return redirect_to "/" if @user.nil?
respond_to do |format|
format.html { render :layout => 'application' }
format.json { render :json => @user.to_json }
end
end
end
The problem is when the user is not found it redirects which works great in via HTML but causes errors with JSON. Is there a clean way to have the redirect only happen if it is a HTML request? What's the right rails, clean way to handle this?
Thanks