在下拉列表中选择值后调用编辑操作

时间:2015-01-12 15:21:09

标签: ruby-on-rails

编辑视图页面包含以下下拉列表:

<%= select_tag "Voiture", options_from_collection_for_select(@vehicles, :id, :model, params[:id].to_i) %> 

使用时,我想重新加载包含下拉列表中Vehicle的页面。我不知道是使用:onchange => submit()还是:action => 'edit'。感谢。

1 个答案:

答案 0 :(得分:0)

假设我们有一个这样的表格(编辑和新动作很常见) 并动态地我们将ID分配给此表单,因此对于编辑操作,这将形成id 是'edit_customer_form'

  #_form.html.erb 
<%= form_for(@customer, html: {id: params[:action] + '_' +'customer_form'}) do |f| %>

  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>

 <div class="field">
    <%= f.label :vehicle_id %><br>
    <%= f.select  :vehicle_id, 
       options_from_collection_for_select(@vehicles, :id, :name) %>
  </div>

    <%= f.submit %>
<% end %>

现在,我们希望当选择框中的值被更改时,编辑表单(#edit_customer_form)应该被提交。

 #application.js

$(document).ready(function(){

 $('#edit_customer_form').on('change', '#customer_vehicle_id', function(){
   $('#edit_customer_form').submit();
 });

});

此处#edit_customer_form是编辑表单ID,#customer_vehicle_id是选择框的ID。 检查以下要点链接中的完整代码:

https://gist.github.com/AjayROR/0a633ee493d78ff30332