Rails: respond_to JSON and HTML

As per my knowledge its not necessary to "render show" in format.html it will automatically look for a respective action view for ex : show.html.erb for html request and show,js,erb for JS request.

so this will work

respond_to do |format|

  format.html # show.html.erb
  format.json { render json: @user }

 end

also, you can check the request is ajax or not by checking request.xhr? it returns true if request is a ajax one.


Yes, you can change it to

respond_to do |format|
  format.html
  format.json { render json: @user }
end