How do I make a json request to a rails 3 website? - ruby-on-rails

I know that if I have a url like
mysite/posts/1
The default type returned to me is html. I can get an xml version of the resource by just doing
mysite/posts/1.xml
But how do I get a json version? Is the following supposed to work?
mysite/posts/1.json
Reason I ask is because it doesn't seem to be working. So I figured I should find out if it's "supposed" to work this way before investigating further.

You're doing it right, but if the Controller isn't setup to respond to json requests you won't get anything. You'll have a respond_to block like this:
respond_to do |format|
format.html
format.xml { render :xml => #model_var.to_xml }
format.json { render :json => #model_var.to_json } #without this line, .json requests will go unanswered by the web server.
end

Related

Rails: Is it neccessary to wrap my POST response in a respond_to block?

I have an AJAX form that sends a POST request to the controller. The controller responds in JSON.
Here, reponse is JSON:
def send_form_response(response)
render json: response
end
The above works fine but I keep seeing examples that use respond_to. My form still works when I wrap my response in the respond_to block.
def send_form_response(response)
respond_to do |format|
format.json { render json: response }
end
end
Does using respond_to give me any benefits? Will anything bad happen if I don't? Or does it make no difference in this case?
respond_to is used to handle multiple responses in the controller#action
If the client wants HTML in response to this action, just respond as
we would have before, but if the client wants XML, return them the
list of people in XML format." (Rails determines the desired response
format from the HTTP Accept header submitted by the client.)
Say for example, If you want send_form_response(response) to respond with HTML and JSON, then you would do it like this
def send_form_response(response)
respond_to do |format|
format.html
format.json { render json: response }
end
end
You can do the same with respond_with
respond_to :html, :xml, :json
def send_form_response(response)
respond_with response
end
So, to answer your questions
Does using respond_to give me any benefits?
Not in your case, where you are requesting only one response
Will anything bad happen if I don't?
Not in your case, no.
Does it make no difference in this case?
No, not at all.

How to send (model object as) reponse in xml format in ruby on rails?

I'm very new to Ruby and Ruby on Rails and I have been searching for a way to send response in xml format via REST API that I am building using ruby on rails. No luck so far!
This is the code I have at the moment:
respond_to do |format|
format.json do
render json: { terminals: #terminals }
end
format.xml do
render xml: { terminals: #terminals }.to_xml
end
end
It kind of works but the output is something I cannot make sense of:
<?xml version="1.0" encoding="UTF-8"?>
<hash>
<terminals type="Terminal">#<Terminal:0x007f1db02b6900></terminals>
</hash>
I don't even know how to search for this issue online. I've tried going through the documentation be I've found no solution. Really need some help with this one! Thanks in advance!
You don't need to call to_xmlon the object that you want to render. If you use the :xml option, render will automatically call to_xml for you.
respond_to do |format|
format.xml {render :xml => #terminals}
end
or in Rails 4 simply:
render :xml => #terminals
http://api.rubyonrails.org/classes/ActionController/MimeResponds.html#method-i-respond_to

Rails respond_to redirect all requests except X mime-type

Below is the typical respond_to block im using in my controller
respond_to do |format|
format.html # show.html.erb
end
I want to restrict all mime-types except html(lets say).
Couldn't come up with a solution, how is this possible? This block does nothing if the request is json, this is OK but what I want is to redirect any requests that are not html.
Thanks
The format object yielded by respond_to has all of the usual mime types (html, js, xml, etc), and it also has a catch-all mime type any that will handle everything else. So, in this case:
respond_to do |format|
format.html
format.any { redirect_to :foo }
end
will use default rendering for html, and will redirect for everything else. See the docs for (a tiny bit) more information: http://apidock.com/rails/ActionController/MimeResponds/respond_to

Understanding Ruby Syntax [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
What is the best way to learn Ruby?
Explain Iterator Syntax on Ruby on Rails
I'm still learning ruby, ruby on rails and such. I'm getting better at understanding all the ruby and rails syntax but this one has me a little stumped.
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #contact_lists }
end
respond_to is a method that takes a proceedure, I think. The two formats look like they may be method calls too, but I don't know.
respond_to is a method which takes block. The block takes one argument, which here is called format.
Now you call two methods on format. html which you call without arguments. And xml which you call with a block.
This block takes no arguments and contains a call to the render method with a hash as an argument. The hash contains the key :xml and the value #contact_lists.
Yeap, you're right.
Ruby method calls are a bit puzzling at first, because you can ommit the parethesis, and they may receive code blocks.
So, this is the explaination:
respond_to do |format|
Invoke the method respond_to and pass it a block on what to do with the format it will receive.
format.html # index.html.erb
With that object called format invoke the method html
format.xml { render :xml => #contact_lists }
And the method xml which in turns receive another block ( do / en and { } , are different syntax to pass block. )
end
Finish the first block
See this other , other answers.
I think this post can help you.
Also, take a minute to read the respond_to documentation.
It is worth to know that this method has changed in Rails 3.
Without web-service support, an action
which collects the data for displaying
a list of people might look something
like this:
def index
#people = Person.find(:all)
end
Here’s the same action, with
web-service support baked in:
def index
#people = Person.find(:all)
respond_to do |format|
format.html
format.xml { render :xml => #people.to_xml }
end
end
What that says is, "if the client
wants HTML in response to this action,
just respond as we would have before,
but if the client wants XML, return
them the list of people in XML
format." (Rails determines the desired
response format from the HTTP Accept
header submitted by the client.)
Supposing you have an action that adds
a new person, optionally creating
their company (by name) if it does not
already exist, without web-services,
it might look like this:
def create
#company = Company.find_or_create_by_name(params[:company][:name])
#person = #company.people.create(params[:person])
redirect_to(person_list_url)
end
Here’s the same action, with
web-service support baked in:
def create
company = params[:person].delete(:company)
#company = Company.find_or_create_by_name(company[:name])
#person = #company.people.create(params[:person])
respond_to do |format|
format.html { redirect_to(person_list_url) }
format.js
format.xml { render :xml => #person.to_xml(:include => #company) }
end
end

rails controller defaults to respond with application/xml in production

I have a standard contacts_controller.rb with index action that responds as follows:
respond_to do |format|
format.html
format.xml { render :xml => #contacts }
end
In development, it works as intended: when I browse to http://localhost:3000/contacts, I get an html response.
But, when I start the app using capistrano on a remote Ubuntu server and browse to the same url, I get an xml response.
If I go to http://remote_host:8000/contacts.html, then I see the html response. If I comment out the format.xml { render :xml => #contacts }, then I see the desired html response.
Pretty sure I'm missing something subtle about difference between Rails development and production modes. Any ideas about what I'm overlooking?
Thanks,
- Dave
The following links explain the phenomenon:
http://rails_security.lighthouseapp.com/projects/15332/tickets/5-using-http-basic-authentication-with-ie-not-working
http://geminstallthat.wordpress.com/2008/05/14/ie6-accept-header-is-faulty/
I fixed my issue by adding this to development|test|production.rb:
config.action_controller.use_accept_header = false

Resources