user1899082
user1899082

Reputation:

How not to pass the format ( json, html, xml ) to the REST URL

I am using Rails 3 and I am using this new Rails 3 thing that it can do with respond_to and respond_with, so it my controller I have something like this:

http://davidwparker.com/2010/03/09/api-in-rails-respond-to-and-respond-with/

respont_to :json

def show

    @organization = Organization.includes([:tableNamesBlah]).find(params[:id])

    respond_with(@organization)

  end

and I am passing it to a view with JBUILDER,....

so in the URL If I go to

http://localhost:3000/manager/1.json

it works fine and returns the data in JSON format but if I go to the same URI without specifying .json as the end of it then it won't return anything.

How can I modify my code in a way that I can see the data but don't need to type .json at the end of the URL ?

Is it something I should do in controller? or should I change the way JBUILDER is processing the data or is it something with the routes? Please let me know if you need to see other parts of the code

Upvotes: 2

Views: 516

Answers (1)

zkcro
zkcro

Reputation: 4344

If you're looking to set a default format, you can set it in your routes.rb file:

resources :things, defaults: { format: :json }

This should give you the behaviour you want (if I'm understanding your question correctly):

/things/1       # returns json
/things/1.json  # returns json
/things/1.html  # returns html

Upvotes: 3

Related Questions