2013-02-14 66 views
1

我已经获得了订单的表单,用户必须选择一个类别,然后选择关联的产品。正如我希望它是动态的,我写了这个:带导轨的窗体中的表格

_form.html.erb

<%= form_for(@order) do |f| %> 

    <%= f.collection_select :category_id, @categories_list, :id, :name, :prompt => "Selectionner" %> 

    <%= render :partial => 'products' %> 

    <%= f.submit 'Enregistrer', :class=>'button add'%> 

<% end %> 

_products.html.erb:

<%= form_for(Order.new, :remote => true) do |f| %> 
    <% if [email protected]? %> 
    <%= f.label 'Produit :' %><br /> 
    <%= f.select :product_id, @products.collect{ |s| [s.name,s.id]}, :prompt => "Selectionner" %> 
    <% else %> 
    <%= f.label 'Produit :' %><br /> 
    <%= f.select :product_id, '' %> 
    <% end %> 
<% end %> 
控制器

def update_product_select 
    @products = Product.where(:category_id=>params[:id]).order(:name) unless params[:id].blank? 
    render :partial => "products", :layout => false, :locals => { :products => @products } 
end 

对于动态部分,它起作用。 但是,当我点击提交按钮时,产品ID不会被发送!

你能告诉我怎么把动态菜单和表单提交结合起来吗?

谢谢。

+1

我不相信你可以在HTML中嵌套表单:http://stackoverflow.com/questions/379610/can-you-nest-html-forms – Andorbal 2013-02-14 14:37:52

+0

所以,在你看来,对我来说最好的办法是什么在表单中有动态菜单? – eluus 2013-02-14 14:44:54

+0

我认为@ mathiewgagne的questinos是很好的。你试图达到什么样的工作流程? – Andorbal 2013-02-14 14:57:49

回答

0

我不明白你为什么需要两种形式。这里不对劲。

如果可能,请详细解释一下工作流程。

我看你应该使用fields_for如果你正在编辑@order的其他相关实例。

对于动态菜单,请在基于类别更新产品列表的类别上绑定javascript事件。让我知道你是否需要一个例子。

+0

其实我从这里拿出来了:http://www.petermac.com/rails-3-jquery-and-multi-select-dependencies/,但是使用formstatic没有,这是它的原因吗?我会很高兴看到一些例子请 – eluus 2013-02-14 14:58:47

+0

这个例子是3岁。我怀疑这是因为你没有使用formstatic。对于这个例子,你实际上在你的教程中有必要的jQuery,它仍然有效。 – mathieugagne 2013-02-14 15:07:07

+0

对于工作流程:我有一个产品表,它链接到类别(belongs_to/has_many)。我想要我的订单中的用户,选择一个类别,然后选择产品(产品belongs_to订单)。我希望它是动态的,因为用户更容易选择它。 – eluus 2013-02-14 15:26:45