wwli
wwli

Reputation: 2681

rails render format- why two formats

This is the code generated by scaffolding

  def index
    @swimming_classschedules = Swimming::Classschedule.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @swimming_classschedules }
    end
  end

Why it is generated with two formats . It seems I only use html format. When does json format get used?

Upvotes: 1

Views: 257

Answers (3)

Ganesh Kunwar
Ganesh Kunwar

Reputation: 2653

Sometimes we need the data in the format of json. Ruby on Rails is one of the must used framework as backend of mobile application (android, ios). The android and ios support data in the format of json. so the data of json format is generated when scaffloding. In case of web application it is not necessary to generate data in the format of json.

Upvotes: 2

Alistair A. Israel
Alistair A. Israel

Reputation: 6567

I actually find them quite useful when I start doing REST-ful API or AJAX-y web service calls, so I tend to leave them there (unless I'm certain that that particular action would or should never return JSON).

Upvotes: 1

t56k
t56k

Reputation: 7001

It's generated to illustrate how the Rails stack works. See this answer.

Upvotes: 1

Related Questions