Low*_*ain 9 json ruby-on-rails form-for
我目前有一个这样的表格:
<% form_for @stem, :html => {:multipart => true} do |f| %>
<%= f.file_field :sound %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
这输出(基本上):
<form method="post" id="new_stem" class="new_stem" action="/stems">
<input type="file" size="30" name="stem[sound]" id="stem_sound">
</form>
Run Code Online (Sandbox Code Playgroud)
但是我打算在这里使用jQuery的ajaxForm插件,并希望以JSON格式返回新的词干.我知道如果表单的动作是"/stems.json"这会起作用,但是有一个参数可以放入form_for调用,要求它返回JSON吗?
我试过了
<% form_for @stem, :html => {:multipart => true, :action => '/stems.json'} do |f| %>
Run Code Online (Sandbox Code Playgroud)
但这似乎不起作用.
lam*_*tor 14
只需指定数据类型:
= form_for(@stemp, :remote => true, :html => {:'data-type' => 'json', :multipart => true})
Run Code Online (Sandbox Code Playgroud)
小智 11
更清洁:
<% form_for @stem, :html => {:multipart => true}, :url => stems_path(:format => "json") do |f| %>
blublublub
<% end %>
Run Code Online (Sandbox Code Playgroud)
<% form_for @stem, :html => {:multipart => true}, :url => "/stems.json" do |f| %>
blublublub
<% end %>
Run Code Online (Sandbox Code Playgroud)
解决了问题