3

Is posible send more than one format, something like:

<%= form_for(@post, :format => :json, :csv, :xml) do |f| %>
  ...
<% end %>

Thank you very much!

2
  • 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. Nov 19, 2012 at 12:17
  • Thank you Rodrigo, can you write a example?. I use button_tag helper for button to submit form.
    – hyperrjas
    Nov 19, 2012 at 12:18

2 Answers 2

3

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:

<%= form_for(@post, :format => :json, :csv, :xml) do |f| %>

...

<%= f.hidden_field :random_param_name, :value => "default format value" %>

// Notice you should use f.submit here instead of button_tag, because it's a form_for, not a form_tag

<%= f.submit "Give me XML!", :onClick => "changeFormat('xml');" %>
<%= f.submit "Give me JSON!", :onClick => "changeFormat('json');" %>
<%= f.submit "Give me CSV!", :onClick => "changeFormat('csv');" %>

<% end %>

Now we add the changeFormat function:

<script type="text/javascript">
    function changeFormat(format) {
        // The ID here is in the format "model_name_field_name". This is default for any form_for.
        $("#post_random_param_name").val(format);
    }
</script>

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:

attr_accessor :random_param_name
attr_accessible :random_param_name
3
  • Thank you but I get syntax error with :format => :json, :csv, :xml.
    – hyperrjas
    Nov 19, 2012 at 14:55
  • Hmm, in which file are you writing this? You should choose only one format, use the parameter to choose. Nov 19, 2012 at 19:09
  • And btw you're getting a syntax error because Ruby can't tell if the comma after :json is for separating the items of the array, like :format => [:json, :csv, :xml] or if it's separating parameters to a method, like (:format => :json), (:csv), (:xml). To correct this ambiguity you should change to :format => [:json, :csv, :xml], but it would still be wrong, because as I said, you can't return more than one format at the same response. Nov 19, 2012 at 19:49
2

Recently, I faced the same problem, "how to send form with desired format".

So I'd found the solution and altered it. Instead of additional parameters I just replaced action attribute with needed URL, because format is the part of URL (it is not the form attribute):

submit_tag "Get JSON", onClick: "$(this).closest('form').attr('action', '#{ my_path(format: :json) }')"

Therefore, you needn't to catch up parameters, use respond_to instead:

respond_to do |format|
  format.json { render json: @my_object.to_hash }
  format.html
end

Hope it will be useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.