【发布时间】:2011-09-20 00:51:15
【问题描述】:
我正在将一个 php 应用程序移植到 rails,但遇到了复杂表单的问题。我有一个名为 menu_headers 的acts_as_tree 模型,并且想创建一个能够在单个页面上编辑菜单的表单。一个菜单可以有无限数量的 menu_headers。
我在 edit.html.erb 中有:这行得通
#this text field works
<%=text_field :menu_header, :name, :index=>@menu_header.id %>
<% if @menu_header.children.empty? %>
<div>no submenus</div>
<% else %>
<ul>
<%= render :partial => 'menu_header_form', :collection => @menu_header.children %>
</ul>
<% end %>
然后在 _menu_header_form.html.erb 中名称正确的地方如 menu_header[3][name] 但值不正确 - 它是上一节中 name 的值。问题是我如何(我可以?)将输入标签的名称与值分离?也许通过选项?
# this is text field is problematic one; want to get menu_header_form.name into the
# value attribute of the tag
<%=text_field "menu_header", :name, :index=>menu_header_form.id %>
<% if menu_header_form.children.empty? %>
<div>no submenus</div>
<% else %>
<ul><%= render :partial => 'menu_header_form', :collection => menu_header_form.children %>
there are submenus
</ul>
<% end %>
如何在上面的 text_field 中指定正确的值?
谢谢
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 form-for