2014-09-20 39 views
1

在我看來代碼:如何創建動態下拉

<%= form_for(:offer,:url=>{:controller=>'offers',:action=>'create'}) do |f| %> 

    <%= f.label "Select Category:" %> &nbsp;<%= f.select :catId_get, options_from_collection_for_select(@categories, "id", "name"), prompt: "Select Category"%> 

    <%= f.label "Select Menu" %> &nbsp;<%= f.select :catId_get, options_from_collection_for_select(@menus, "id", "menu_item_name"), prompt: "Select Menu Item"%> 

在控制器:

@categories = Category.where(:hotel_id=>cookies[:hotel_id_for_login_user]) 
@menus = Menu.where(:category_id=>params[:catId_get]) 

我在rails.I新希望顯示在菜單的下拉菜單推崇當我選擇類別下拉列表。請幫助我。謝謝。

+0

你必須使用'onchange'方法。搜索一下。和一個類似的問題:http://stackoverflow.com/questions/8690516/select-and-onchange-in-a-ruby-on-rails-form – mgh 2014-09-20 09:47:43

+0

可能重複的[連鎖選擇框中的Rails 3](http:///stackoverflow.com/questions/5302470/chained-select-box-in-rails-3) – 2014-09-21 22:15:43

回答

0

這應該動態地通過Ajax以這樣的方式進行:

$(".category-select").change(function(){ 
    $.get("/menus?category_id=1", function(data) { 
    $.each(data, function(key, val) { 
    $(".menu-select").append('<option value="' + val.whatever + '">' + val.whatever + '</option>'); 
    }); 
    }); 
}) 

或者類似的規定。