2011-09-20 16 views
0

我正在將一個php應用程序移植到rails中,並且在複雜的表單中遇到問題。我有一個名爲menu_headers的acts_as_tree模型,並且希望創建一個能夠在單個頁面上編輯菜單的窗體。一個菜單可以有不限數量的menu_headers。Rails 3.1 acts_as_tree,表單和值在單個頁面上編輯菜單

我在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] [名],但該值不正確 - 它是上一節中名稱的值。問題在於,我如何(可以?)將輸入標記的名稱從值中分離出來?也許通過一個選項?

# 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中指定正確的值?

THX

回答

0

可以用該值作爲像這樣的選項更新: <%= text_field:menu_header,:姓名,:指數=> menu_header_form.id,:值=> menu_header_form.name%>

其中修復了在控制器中聲明的menu_header問題。