Rails 3.1 acts_as_tree、表单和值在单个页面上编辑菜单
我正在将 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 %>
我如何在上面的文本字段中指定正确的值?
谢谢
I'm porting a php app to rails and am having trouble with a complex form. I have a acts_as_tree model called menu_headers and would like to create a form that is able to edit a menu on a single page. A menu could have an unlimited number of menu_headers.
I have in edit.html.erb: this works
#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 %>
and then in _menu_header_form.html.erb where the name is correct such as menu_header[3][name] but the value is incorrect - it is the value of name in the previous section. The issue is how do I (can I?) decouple the name of the input tag from the value? Perhaps via an options?
# 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 %>
How would I specify the correct value in the text_field above?
thx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可以使用该值作为选项进行更新,如下所示:
<%=text_field :menu_header, :name, :index=>menu_header_form.id, :value=>menu_header_form.name %>
修复了控制器中声明的 menu_header 不可见的问题。
Could update with the value as an option like this:
<%=text_field :menu_header, :name, :index=>menu_header_form.id, :value=>menu_header_form.name %>
which fixed the issue of the menu_header declared in the controller from being seen.