form_for 带有 json 返回

发布于 2024-08-31 18:16:19 字数 659 浏览 2 评论 0原文

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

<% 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| %>

但这似乎不起作用。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

↘紸啶 2024-09-07 18:16:20
<% form_for @stem, :html => {:multipart => true}, :url => "/stems.json" do |f| %>
blublublub
<% end %>

解决了问题

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

solves the problem

一生独一 2024-09-07 18:16:19

只需指定数据类型:

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

Just specify the data-type:

= form_for(@stemp, :remote => true, :html => {:'data-type' => 'json', :multipart => true})
无力看清 2024-09-07 18:16:19

更干净:

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

even cleaner:

<% form_for @stem, :html => {:multipart => true}, :url => stems_path(:format => "json") do |f| %>
  blublublub
<% end %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文