【问题标题】:Rails 3.1 acts_as_tree, form and values to edit a menu on a single pageRails 3.1acts_as_tree,表单和值在单页上编辑菜单
【发布时间】: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


    【解决方案1】:

    可以像这样使用该值作为选项进行更新: menu_header_form.id, :value=>menu_header_form.name %>

    修复了控制器中声明的 menu_header 不被看到的问题。

    【讨论】:

      猜你喜欢
      • 2014-02-20
      • 2011-11-15
      • 2011-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-09
      • 1970-01-01
      相关资源
      最近更新 更多