Rails 3.1中的嵌套表单 [英] Nested forms in rails 3.1

查看:73
本文介绍了Rails 3.1中的嵌套表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对嵌套表单有一个问题:rails 3.1在应有的时候(例如,在编辑现有记录时)不会渲染fields_for块.由于我对英语的信心不足,因此我制作了一个小示例应用程序:

I have a problem with nested forms: rails 3.1 doesn`t render fields_for blocks when it should (when editing existing record for example). Since I`m not confident enough in my english, I`ve made a small example app:

新动作:

def new
  @manga = Manga.new
  3.times {@manga.volumes.build}
end

表单代码:

<%= form_for @manga do |f| %>
  <%= f.error_messages %>
  <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </p>
  <% f.fields_for :volumes do |builder| %>  
  <p>  
    <%= builder.label :cover_link, "Link to cover" %><br />  
    <%= builder.text_field :cover_link %>  
  </p>  
  <% end %>  
  <p><%= f.submit %></p>
<% end %>

在Rails 3.0中,生成的表单看起来像一样. (好的,所以我不能发布图像.因此,我不得不将其作为链接.)但是在rails 3.1中,结果为不同.

In Rails 3.0 the resulting form looks just like it should. (OK, so I can't post images. So I have to put them as links instead.) But in rails 3.1 the result is different.

我可能还应该注意,我在保存方面没有任何问题,并且大多数其他类似的问题都可以提出.当从railscast 197用JS添加字段时,所有内容都可以完美保存.这里的主要问题是保存的所有内容都无法编辑.

I probably should also note, that I`ve no problems with saving and whatever else most similar questions ask. Everything is saved perfectly, when fields are added with JS from railscast 197. The main problem here is that everything saved is impossible to edit.

推荐答案

在潜行之后,我发现我的问题与其他问题没什么不同.

And after lurking around I found out, that my question was not that different from the others.

不赞成使用fields_for调用方式.

Deprecated way to call fields_for was the fault.

事实是通过JS保存了数据(并添加了字段),这有点误导了我.解决方案非常简单:

The fact that data was being saved (and fields were being added) through JS, kind of mislead me. Solution was quite simple:

not `<% fields_for %>`, but `<%= fields_for %>`

这篇关于Rails 3.1中的嵌套表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆