0

这是我目前的 _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

结尾

如何在选择区域下拉列表值时以 ajax 方式更新表单?

现在,这是我的 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

结尾

我还必须使用 Prototype-Rails 来使 remote_function 工作。我的字段仍然没有更新,我在这里缺少什么..

4

2 回答 2

0

You need to use OnChange option of select tag

This should help [ How to add an onchange event to select tag in 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- ]

Edit:

Explanation: When you will select any option of your dropdown, the action provided in OnChange will be executed. Now you can make a ajax call to your server to get the values that you want to update your fields with. To do that you need remote_function ( described below).

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. This should contain a remote_function which will be like following

: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. So put all your :city_id, :mobile, :telephone fields in a HTML node with unique id that you will use here.
:method: just use as defined in your route. May be GET.
:with: Some extra attributes to supply with the request.
:url: The URL where you have to request.

If this still doesnt help, please share how you are trying. May be a small fix can solve the problem.

于 2012-08-23T08:18:39.560 回答
0

如果您使用带有 jquery 的 rails,您可以尝试以下
http://samuelmullen.com/2011/02/dynamic-dropdowns-with-rails-jquery-and-ajax/

虽然我自己没有尝试过,但听起来很有趣。

于 2012-08-23T10:05:21.770 回答