1

I've got a form for an order and with this, the user have to choose a category then associated product. As I want it to be dynamic, I wrote this :

_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 !@products.blank? %>
    <%= 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 %>

in the controller :

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

For the dynamic part, it works. But when I click on the submit button, the product ID is not sent !

Can you tell me how is it possible for me to combine dynamic menu and form submitting please ?

Thanks.

eluus
  • 213
  • 1
  • 13

2 Answers2

0

I don't understand why you need two forms. Something is wrong here.

Explain a bit more on the workflow if possible.

I see you should either make use of fields_for if you are trying to edit other associated instances of @order.

For a dynamic menu, have a javascript event binding on the category that updates the products list based on the category. Let me know if you need an example.

mathieugagne
  • 2,465
  • 1
  • 17
  • 18
  • In fact I took exemple from here : http://www.petermac.com/rails-3-jquery-and-multi-select-dependencies/ but whitout using formstatic, is it the reason ? I will be happy to see some example please – eluus Feb 14 '13 at 14:58
  • The example is 3 years old. I doubt it's because you're not using formstatic. And for the example, you actually have the necessary jQuery in your tutorial, which is still valid. – mathieugagne Feb 14 '13 at 15:07
  • For the workflow : I have a table with product, which is linked to categories (belongs_to / has_many). I want user in my order form, to choose a category and then the product (product belongs_to order). I want it dynamic because it is easier for the user to choose it. – eluus Feb 14 '13 at 15:26
0

I think you are looking for this: http://railscasts.com/episodes/88-dynamic-select-menus-revised It needs a subscription, but you can also see the previous episode to get an idea if you dont want to subscribe.

Hugo
  • 2,069
  • 22
  • 23