Lowgain
Lowgain

Reputation: 3294

form_for with json return

I currently have a form like this:

<% form_for @stem, :html => {:multipart => true} do |f| %>
<%= f.file_field :sound %>
<% end %>

This outputs (essentially):

<form method="post" id="new_stem" class="new_stem" action="/stems">
<input type="file" size="30" name="stem[sound]" id="stem_sound">
</form>

However I'm planning to use jQuery's ajaxForm plugin here and would like the new stem to be returned in JSON format. I know if the form's action was "/stems.json" this would work, but is there a parameter I can put into the form_for call to ask it to return JSON?

I tried doing

<% form_for @stem, :html => {:multipart => true, :action => '/stems.json'} do |f| %>

but this didn't appear to work.

Upvotes: 10

Views: 15544

Answers (3)

alup
alup

Reputation: 2981

even cleaner:

<% form_for @stem, :html => {:multipart => true}, :url => stems_path(:format => "json") do |f| %>
  blublublub
<% end %>

Upvotes: 11

lambinator
lambinator

Reputation: 11009

Just specify the data-type:

= form_for(@stemp, :remote => true, :html => {:'data-type' => 'json', :multipart => true})

Upvotes: 15

Lowgain
Lowgain

Reputation: 3294

<% form_for @stem, :html => {:multipart => true}, :url => "/stems.json" do |f| %>
blublublub
<% end %>

solves the problem

Upvotes: 2

Related Questions