Reputation: 2819
I am new to Ruby on Rails
In my project, I request to get JSON
and HTML
format on index
. But I want to check authentication
for HTML
request only not JSON
request.
I used devise
gem, for authentication
.
on my Controller
I set
skip_before_action :authenticate_user!, only[:index], if: -> { request.format.json? }
def index
@products = Product.all
respond_to do |format|
format.html
format.json { render :json => @products }
end
end
Currently JSON
and HTML
request doesn't ask for authentication
How do I achieve this?
Is there any way to check authentication
on format.html { ... }
Thanks in advance
Upvotes: 0
Views: 1095
Reputation: 41
Try this:
skip_before_action :authenticate_user!, if: :skip_authenticate_user
def index
...
end
protected
def skip_authenticate_user
action_name=='index' && (request.format.json? || request.format.xml?)
end
Upvotes: 1