Deekor
Deekor

Reputation: 9499

disable JSON rendering of an object

I used a scaffold to build a number model. I've noticed that if I go to http://localhost:3000/numbers/3.json it will render the JSON for that number. All the action in the controller says is:

  def show
  end

and the only route is:

resources :numbers

Where is rails being told that its ok to accept a JSON request and render the JSON?

Upvotes: 0

Views: 881

Answers (3)

BrunoF
BrunoF

Reputation: 3523

You can also disable JSON requests at routes.rb, using constraints:

# Allow a HTML only
resources :numbers, constraints: {format: :html}

Upvotes: 1

C404
C404

Reputation: 243

Cause if you don't specify format, rails will just adapt.

cf: /numbers/:id(.:format)

Just add respond_to to your show method

respond_to do |format|
 format.html
end

Hope it helped

Upvotes: 0

Deekor
Deekor

Reputation: 9499

There were JSON views created by the scaffolding. For example there was a file: app/views/numbers/show.json.jbuilder which was rendering the JSON

Upvotes: 1

Related Questions