Jquery RubyonRails集合的选择更改取决于表单中的其他更改

Jquery RubyonRails集合的选择更改取决于表单中的其他更改,jquery,ruby-on-rails,ajax,Jquery,Ruby On Rails,Ajax,我正在做一个项目,允许客户在RubyonRails上预订酒店房间。我希望客户指定他想要的房间类型,并根据他的选择,显示此类型的房间列表 <div class="form-group"> <div class="control-label col-sm-5"> <%= f.label :room_Type %> </div> <div class="col-sm-6"> <%= f.colle

我正在做一个项目,允许客户在RubyonRails上预订酒店房间。我希望客户指定他想要的房间类型,并根据他的选择,显示此类型的房间列表

    <div class="form-group">
    <div class="control-label col-sm-5">
    <%= f.label :room_Type %> </div>
    <div class="col-sm-6">
    <%= f.collection_select :room_type, Type.all, :id, :name, :prompt =>  true  %></div></div>
    <div class="form-group">
    <div class="control-label col-sm-5">
    <%= f.label :room_Number %> </div>
    <div class="col-sm-6">
    <%= f.collection_select :room_id, Room.all, :id, :number, :prompt => true  %>
    </div></div>

现在,我可以显示数据库中的房间类型列表。我还可以显示数据库中的房间列表。我对此做了一些搜索,他们说可以使用JQuery或Ajax来完成,但我不知道怎么做。你能帮我一下吗?

这个解决方案也可以帮助你理解流程。 首先,向控制器发出ajax调用。请记住,来自ajax调用的url必须存在于路由中

$(document).ready(function() {
  $("#room_type").on('change', function(){
    $ajax({
      url: "populate_other_list",
      type: "GET",
      data: {room_type: $(this).val()},
      // Callbacks that will be explained
    })
  });
接下来,在控制器内执行操作

def populate_other_list
  room_type = params[:room_type]
  @room = Room.find_by room_type: room_type
or    
  Pass Id from AJax of Type and find the room here from Type    
  @room = Type.find(params[:type_id).rooms // Call the association if exists    
  respond_to do |format|
    format.json { render json: @room }
  end
end
这样,在成功回调ajax调用时,您将获得一个带有列表的JSON对象。因此,您需要清除房间号,选择并创建选项并附加到房间号

// Ajax call
success: function(data) {
  $("#room_id").children().remove();
  // Create options and append to the list
}
// Rest of Ajax call
如您所见,我没有将创建选项的代码放在

因为它的模拟javascript很容易在stackoverflow中找到 提示如下所示

$("#room_id").append("<option>" + /your value/ + "</option>");

此解决方案还可以帮助您了解流程。 首先,向控制器发出ajax调用。请记住,来自ajax调用的url必须存在于路由中

$(document).ready(function() {
  $("#room_type").on('change', function(){
    $ajax({
      url: "populate_other_list",
      type: "GET",
      data: {room_type: $(this).val()},
      // Callbacks that will be explained
    })
  });
接下来,在控制器内执行操作

def populate_other_list
  room_type = params[:room_type]
  @room = Room.find_by room_type: room_type
or    
  Pass Id from AJax of Type and find the room here from Type    
  @room = Type.find(params[:type_id).rooms // Call the association if exists    
  respond_to do |format|
    format.json { render json: @room }
  end
end
这样,在成功回调ajax调用时,列表中会出现一个JSON对象。因此,您需要清除房间号,选择并创建选项并附加到其中

// Ajax call
success: function(data) {
  $("#room_id").children().remove();
  // Create options and append to the list
}
// Rest of Ajax call
如您所见,我没有将创建选项的代码放在

因为它的模拟javascript很容易在stackoverflow中找到 提示如下所示

$("#room_id").append("<option>" + /your value/ + "</option>");

谢谢你的解释。我还有其他问题。我应该先把ajax调用放在哪里,成功后再把ajax调用放回哪里。你的意思是我像其他页面一样使用get在routes中定义url?是的,你需要在routes中将其定义为get,并在assets/javascript目录中编写代码。否则,你可以使用预定义的rails ajax格式好的,我现在理解这个想法了。谢谢你的帮助,Tushar如果你愿意,请在这里发布任何问题谢谢你的解释。我还有其他问题。我应该先把ajax调用放在哪里,成功后再把ajax调用放回哪里。你的意思是我像其他页面一样使用get在路由中定义url?是的,你需要在路由中定义它在assets/javascript目录下获取并编写代码。否则,您也可以使用预定义的rails ajax格式。我现在就明白了。感谢您的帮助,如果您遇到任何问题,请在这里发布