Reputation: 85
I want to send an XML file when get request is made
here is my my xml file
<forms>
<form url="https://opendatakit.appspot.com/formXml?formId=CascadingSelect">Cascading Select Form</form>
<form url="https://opendatakit.appspot.com/formXml?formId=widgets">Widgets</form>
</forms>
this is my url
http://localhost:3000/forms/formlist
here is my ruby controller to send a xml
respond_to :xml
def formlist
respond_to do |format|
format.xml {//what should I write here to send formlist.xml}
end
end
here is route.rb
get 'forms/formlist' => 'forms#formlist'
but its not sending xml file giving error code something 422
Please help me
Thanks in advance.
Upvotes: 0
Views: 957
Reputation: 48589
respond_to do |format| format.xml {//what should I write here to send formlist.xml} end
You don't have to write anything between the braces--if your xml file has the correct name.
First, your route can be simply:
get "forms/formlist"
By default, rails assumes that a route has the format controller/action
.
Your formlist() action can simply look like this:
def formlist
respond_to :xml
end
The respond_to
line is a shortcut for:
respond_to do |format|
format.xml {}
end
In either case, your action will only respond to requests for an xml file; requests for other file types will produce an UnknownFormat error.
Then create the xml file here:
app/views/forms_controller/formlist.xml.erb
If you don't have any ruby code in your xml file, you can omit the .erb extension.
To request the xml file, you can use the url:
http://localhost:3000/forms/formlist
...and specify the following header in the request:
Accept: text/xml
For instance:
curl --header 'Accept: text/xml' http://localhost:3000/forms/formlist
Or, as rubykid posted, you can use the url:
http://localhost:3000/forms/formlist.xml
For instance:
curl http://localhost:3000/forms/formlist.xml
In either case, rails understands that the request is asking for an xml file.
By default, an action renders an html file named:
app/views/some_controller/action_name.html.erb
The default rendering can be changed with respond_to()
and/or render()
.
For example, respond_to :xml
will render the file:
app/views/some_controller/action_name.xml.erb
...which in your case will be:
app/views/forms_controller/formslist.xml.erb
If you want to render a different xml file, only then do you need to use the block form of respond_to() and write something inside the braces:
respond_to do |format|
format.xml {render ...}
end
In that case, see the Rails Guide on rendering for what you can do with render().
For completeness sake, the shortcut:
respond_to :xml, :json
...is equivalent to:
respond to do |format|
format.xml {}
format.json {}
end
If the xml file is a static file, i.e. its content never changes, and you want to make it publicly available to anybody, you can put it in the public/
folder. For instance, if you name the xml file:
public/data.xml
...then anyone can request the xml file with the url:
http://localhost:3000/data.xml
Upvotes: 1
Reputation: 321
.xml
to url: http://localhost:3000/forms/formlist.xmlThen simply render your file:
def formlist
respond_to do |format|
format.xml { render '/path/to/formlist.xml' }
end
end
Instead of adding .xml to url you can write your route as this:
get 'forms/formlist' => 'forms#formlist', defaults: { format: 'xml' }
so every request without MIME type will be interpreted as xml request
Upvotes: 2