这是我目前的 _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 工作。我的字段仍然没有更新,我在这里缺少什么..