1

请帮助,我使用了一些示例来构建动态选择,但它似乎不起作用,但是在控制台我可以看到 200 状态,我不流利所以请帮助,问题是选择中的列表不会减少,选择 previos 后显示相同的选项

index.html.erb

  <%= collection_select(nil, :site_id,  @sites,  :id, :name, {:prompt   => "Select a Site"}, { :id => "sites_select"}) %>
                            <br/>
                    <%= collection_select(nil, :floor_id, @floors, :id, :name, {:prompt   => "Select a Floor"}, {:id => "floors_select"}) %>
                    <br/>
                    <%= collection_select(nil, :pod_id, @pods, :id, :name, {:prompt   => "Select a pod"}, {:id => "pods_select"}) %>
                    <script>
                          $(document).ready(function() {
                            $('#sites_select').change(function() {
                              $.ajax({
                                url: "<%= update_floors_path %>",
                                data: {
                                  site_id : $('#sites_select').val()
                                },
                                dataType: "script"
                              });
                            });
                            $('#floors_select').change(function() {
                              $.ajax({
                                url: "<%= update_pods_path %>",
                                data: {
                                  floor_id : $('#floors_select').val()
                                },
                                dataType: "script"
                              });
                            });
                          });
                        </script>

devices_controller.rb

  def update_floors
# updates floors and pods based on genre selected
site = Site.find(params[:site_id])
# map to name and id for use in our options_for_select
@floors = site.floors.map{|a| [a.name, a.id]}.insert(0, "Select a Floor")
@pods   = site.pods.map{|s| [s.name, s.id]}.insert(0, "Select a Pod")

end

def update_pods
# updates pods based on floor selected
floor = Floor.find(params[:floor_id])
@pods =floor.pods.map{|s| [s.name, s.id]}.insert(0, "Select a Pod")
end

update_floors.js.erb

$('#floors_select').html("<%= escape_javascript(options_for_select(@floors)) %>");
$('#pods_select').html("<%= escape_javascript(options_for_select(@pods)) %>");

update_pods.js.erb $('#pods_select').html("<%= escape_javascript(options_for_select(@pods)) %>");

路线.rb

  get 'devices/update_floors', :as => 'update_floors'
      get 'devices/update_pods', :as => 'update_pods'
      root :to => "devices#index"

感谢任何建议谢谢你,D

4

3 回答 3

1

问题出在你的控制器上,

您没有在代码中的任何地方提到 respond_to js 块,

只需将此代码放在两个更新方法中,

respond_to do |format|
 format.js
end 
于 2012-12-12T07:39:40.260 回答
1

似乎新加载的 JavaScript 没有向父页面添加效果。为什么不在 Ajax 成功回调中执行 $().html。并使 update.js.html 页面只包含 select 的选项。应该管用。

javascript:

$("#sites_select").change(function(){
    $.ajax({
      url: your_url,
      //orther options ...
      success: function(data){
          $("#floors_select").html(data);
      }
    });

});

在你的 update_floors.js.html

<%=options_for_select(@floors)%>

对更新 pod 执行相同的操作。:)

于 2012-12-12T07:45:21.660 回答
0

在控制器中给出响应格式respond_to :js, :only=>[:update_floors,:update_pods ]
除此之外,将以下内容写入update_floors.js.erb文件。

$('#floors_select').html("<%= escape_javascript(collection_select(:your_object_name, :floor_id, @floors.to_a, :id, :name, :prompt=>"Select a Floor")) %>");
我认为上面的方法会起作用。

于 2012-12-12T07:48:22.940 回答