form_for与json返回

时间:2010-05-02 23:49:02

标签: ruby-on-rails json form-for

我目前有一个这样的表格:

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

这输出(基本上):

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

但是我打算在这里使用jQuery的ajaxForm插件,并希望以JSON格式返回新的词干。我知道如果表单的动作是“/stems.json”这会起作用,但是我可以在form_for调用中放入一个参数来要求它返回JSON吗?

我试过

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

但这似乎不起作用。

3 个答案:

答案 0 :(得分:14)

只需指定数据类型:

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

答案 1 :(得分:11)

更干净:

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

答案 2 :(得分:2)

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

解决问题