Is posible send more than one format, something like:
<%= form_for(@post, :format => :json, :csv, :xml) do |f| %>
...
<% end %>
Thank you very much!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You don’t “send a format”, you send a request to the server, which responds to this request in the specified format. You can use a parameter to tell the controller which format you want it to return.
There are several ways to tell the controller which format you want. Actually there’s only one way, passing a parameter, but there are several ways to pass this parameter.
I like the hidden_field method, in which you add a hidden field with the value being the format you want, and change the value of this field using javascript when a user selects a radio button with the format, for example. You can also do this using more than 1 submit button, and having the HTML onClick attribute set to a function that changes the value of the hidden_field.
Example (I’ll use the multi-button method, and use jQuery):
First, let’s build the form:
Now we add the changeFormat function:
Now you just need to catch this parameter in the controller (
params[:post][:random_param_name]) and do what you have to do! One last thing you need to do is adding this random_param_name to the accessible attributes of the Post model: