Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Just wondering what the accepted convention is for Rails controller design. Currently, every controller in my app that I've written it set up to send a JSON response when necessary. Thing is, I only ever utilse HTML responses in my app. So is it a good idea to have them defined?

For example:

def show
  @dog = Dog.find(params[:id])

  respond_to do |format|
    format.json { render json: @dog } # needed?

It makes the controller code less readable (because more LOC) and it also means I have to think deeply about what a good JSON response should be when HTML is not being is used, so it decreases the speed of my controller development. This is especially true when you have conditional responses.

For example:

def create
  @dog =[:dog])

  respond_to do |format|
      format.html { redirect_to @dog }
      format.json { render json: @dog, status: :created, location: @dog }
      format.html { render action: "new" }
      format.json { render json: @dog.errors, status: :unprocessable_entity }

The only positive I can see is that it "future-proofs" the controllers (i.e. if I need JSON responses later on then they are already written).

But if I'm writing JSON responses just because then by that same logic I might as well write XML responses (i.e. format.xml) too...

share|improve this question

as it is trivial to add json (or xml, etc) responses later on, I would recommend against implementing them before.
your reasons you give are valid:

  • it is less readable
  • you more LOC and thus more code to maintain, more potential bugs
  • you spend time writing code that you don't need


  • you risk exposing attributes that are not meant to be openly accessible

there is no RoR convention to have all actions respond to json.

share|improve this answer
I am new to Rails and I was also wondering about this. Why do something that I don't need now and may never need. YAGNI is the word that comes to mind. – Lee Apr 21 '13 at 12:21

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.