简体   繁体   English

如何在从Ruby on Rails的下拉列表中执行选择时预填充表单(ajax方式)?

[英]how to pre-populate the form (ajax way) on performing a selection from a dropdown list inside it, Ruby on Rails?

here is my _form.html.erb partial at the moment, but I want its elements to be updated based on the first (region) dropdown selection I make: 这是我目前的_form.html.erb部分,但我希望根据我做出的第一个(区域)下拉选择来更新其元素:

<%= form_for(@setting) do |f| %>
  <div class="form">

     <div class="fieldBlock">
    <%= f.label :region%>
       <%= f.select :region_id, options_from_collection_for_select(Region.all, 'id',  'region_code'), 
:onChange => remote_function(:update => ":mobile, :telephone", :method => "get", :url => { :controller => :Settings, :action => :form_update, :id => :region_id}) %>
   </div>


<div class="fieldBlock">
<%= f.label :city_id, "City" %>
   <%= f.select :city_id, options_from_collection_for_select(City.all, 'id', 'name'), :prompt => 'select a city' %>
</div>


<div class="fieldBlock">
<%= f.label :mobile, "Mobile" %> <%= f.text_field :mobile%></div>

<div class="fieldBlock">
<%= f.label :telephone, "Telephone No"  %> <%= f.text_field :telephone %></div>

<div class="fieldBlock">
<% f.submit "Update Contact" %>

At present this is the controller method that helps in prepopulating:

def index
 setting = Setting.find_by_user_id(current_user.id)

 if setting.blank?
   @setting = City.first.dup

else
   @setting = setting
end

end 结束

how do I update the form in an ajax way on selection of region dropdown list value? 在选择区域下拉列表值时如何以Ajax方式更新表单?

Now, here is my form_update action: 现在,这是我的form_update动作:

 def form_update(id)
 setting = Setting.find_by_region_id(id)

   respond_to do |format|
     if setting.blank?
        @setting = Setting.first.dup
        format.json {@setting}
      else
        @setting = setting
        format.json {@setting}
      end
  end

end 结束

I also had to use Prototype-Rails to make remote_function work. 我还必须使用Prototype-Rails来使remote_function起作用。 Still my fields are not getting updated, what am I missing here.. 我的字段仍然没有更新,我在这里缺少什么。

You need to use OnChange option of select tag 您需要使用select标签的OnChange选项

This should help [ How to add an onchange event to select tag in rails ] 这应该有助于[ 如何添加onchange事件以选择Rails中的标签 ]

A better explanation of using :onChange option of select_tag is here [ http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/select#51-Demo-select-onchange-invoke-an-ajax- ] 有关使用select_tag :onChange选项的更好说明,请select_tag [ http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/select#51-Demo-select-onchange-invoke-an-ajax- ]

Edit: 编辑:

Explanation: When you will select any option of your dropdown, the action provided in OnChange will be executed. 说明:当您选择下拉菜单的任何选项时,将执行OnChange提供的操作。 Now you can make a ajax call to your server to get the values that you want to update your fields with. 现在,您可以对服务器进行ajax调用,以获取要用来更新字段的值。 To do that you need remote_function ( described below). 为此,您需要remote_function (如下所述)。

Considering your example the following snippet should work 考虑您的示例,以下代码段应该有效

<%= form_for(@setting) do |f| %>
    <div class="form">
        <div class="fieldBlock">
        <%= f.label :region%>
        <%= f.select :region_id, options_from_collection_for_select(Region.all, 'id',  'region_code'), 
             :onchange => remote_function( ... ... ...) %>
    </div>
<% end %>

Above I have shown only the dropdown part of your form. 上面我仅显示了表单的下拉部分。 Note the :onchange option here. 注意这里的:onchange选项。 This should contain a remote_function which will be like following 它应该包含一个remote_function ,如下所示

:onchange => remote_function(:update => "message_id",
                             :method => "put",
                             :with => "'item=' + value",  
                             :url => { :controller => :orders, :action => :set_customer_id, :id => order.id}))

Here: 这里:

:update : is the HTML node whose contain will be updated. :update :是HTML节点,其包含将被更新。 So put all your :city_id , :mobile , :telephone fields in a HTML node with unique id that you will use here. 因此,将所有:city_id:mobile:telephone字段放在具有唯一ID的HTML节点中,您将在此处使用该ID。
:method : just use as defined in your route. :method :只需按照您的路线中的定义使用即可。 May be GET. 可能是GET。
:with : Some extra attributes to supply with the request. :with :一些额外的属性来提供请求。
:url : The URL where you have to request. :url :您必须请求的URL。

If this still doesnt help, please share how you are trying. 如果仍然不能解决问题,请分享您的尝试方式。 May be a small fix can solve the problem. 可能是一个小的解决方案可以解决问题。

If you are using rails with jquery, you can try the following 如果您将Rails与jquery一起使用,则可以尝试以下操作
http://samuelmullen.com/2011/02/dynamic-dropdowns-with-rails-jquery-and-ajax/ http://samuelmullen.com/2011/02/dynamic-dropdowns-with-rails-jquery-and-ajax/

Though I didnt try it myself, but sounds interesting. 虽然我自己没有尝试过,但是听起来很有趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM