Ahmad Al-kheat Ahmad Al-kheat - 2 months ago 8
React JSX Question

Should I still respond to html if view is rendered with javascript?

I have a Rails application integrated with react, my views are rendered with react, including the html (JSX). I noticed that I had a few

format.html
responses from back when my views were regular erb views, now that they are not, should I still respond to html just in case (even though I don't see how a user can use my app if they javascript have disabled)?

Example :

def destroy
@comment.destroy
respond_to do |format|
format.json { head :no_content }
format.html { redirect_to @question, notice: 'Comment was deleted.' }
end
end


Can I get rid of the html responses?

Answer

Whether to keep it or not is a personal choice. I sometimes do, but fewer LOC makes for cleaner code. To remove it you have several options. You cna leave the respond_to as is and just remove the html eg:

def destroy
  @comment.destroy
  respond_to do |format|  
    format.json { head :no_content }
  end
  end
end

but you can also remove the respond-to from each action (even fewer LOC) with something like this:

# put this LOC at the top of your controller, outside of any action
respond_with :json

# then each action is much simpler... you just assume it's always json
def destroy
  @comment.destroy
  head :no_content
end