English 中文(简体)
Error 404 for Dynamic Dropdown withrails 3.1 and Ajax (Jquery)
原标题:Error 404 for Dynamic Dropdown with rails 3.1 and Ajax (Jquery)

我在制造动态的衰退时遇到困难:

当你选择一个国家时,它使部分城市属于一个国家。

我有这样的模式:国家拥有——城市,城市属于国家。

随着火力的推移,我在选择联系记录而不是国家记录时,将控制者的行动更新——城市——当选。

I followed the example from Peter Mac

我的守则是:

申请:js

jQuery(function($) {
// when the #region_id field changes
  $("#contact_country_id").live( change , function() {
    // make a POST call and replace the content
    var country = $( select#contact_country_id :selected ).val();
    if(country == "") country="0";
    jQuery.get( /contacts/update_city_select/  + country, function(data){
        $("#cities").html(data);
    })
    return false;
  });
})

联系人:控制员。

  def new
    @contact = Contact.new
    @industries = Industry.all
    @countries = Country.all
    @cities = City.where(["country_id = ?", 1]).all
    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @contact }
    end
  end
.
.
.
  def update_city_select
      @cities = City.where(:country_id => params[:id]).order(:name) unless params[:id].blank?
      render :partial => "cities", :locals => { :cities => @cities }
  end

联系/_form.html.erb

<%= form_for(@contact) do |f| %>
...
    <div class="field">
        <%= f.label :country %>
        <%= f.collection_select :country_id, @countries, :id, :name, :prompt => "-- Select a country --" %>
    </div>
    <div id="cities" class="field">
        <%= render  cities  %>
    </div>
...
<% end %>

联系/单位:html。

<%= fields_for @contact do |f| %>
    <%= f.label :city %>
    <% unless cities.blank? %>
        <%= f.collection_select :city_id, cities, :id, :name, :prompt => "-- Select a city --" %>
    <% else %>
        <%= f.select "city_id","city_id", :prompt => "-- Select a city --" %>
    <% end %>

<% end %>

路线。

  resources :cities
  resources :countries
  resources :contacts
  get  /contacts/update_city_select/:id  =>  contacts#update_city_select 

请你帮助解决阿贾克斯问题!

Aurelien

最佳回答

www.un.org/Depts/DGACM/index_spanish.htm 问题:

从上文提到的辅导中,我认为我不得不在接触者控制员中采用“最新——城市——当选”的方法。 轮船当时认为,它应当从联系记录中提一个项目,而不是国家记录。

www.un.org/Depts/DGACM/index_spanish.htm 解决办法:

The solution for displaying the Ajax correctly me was 1) to nest countries resources, 2) move the method to the countries controller, 3) create a partial "_cities.html.erb" in the countries views, 4) slightly modify the form and the JS.

我在《守则》中将那些可能遇到类似问题或需要回答如何开展有活力的选定工作的人放在后面。

注:我使用铁路3.1号,配有JQuery和NO表格的建筑商或表格

www.un.org/Depts/DGACM/index_spanish.htm 模型

class Contact < ActiveRecord::Base
  belongs_to :city
  belongs_to :country

  attr_accessible :name, :country_id, :city_id
end

class Country < ActiveRecord::Base
  has_many :cities, :dependent => :destroy
  has_many :contacts
  attr_accessible :name, :city_id
end

class City < ActiveRecord::Base
  has_many :contacts
  belongs_to :country
  attr_accessible :name, :country_id
end

<><>控制器仅创造固定的休息形式,仅添加countries_ Controller 以下方法

def update_city_select
    @cities = City.where( :country_id => params[:id]).order(:name) unless params[:id].blank?
    render :partial => "cities", :locals => { :cities => @cities }
end

关于Views/contacts/new.html.erb>,你将需要两件事:为各国提供 Collection_select,以提及部分,该部分内容将放在意见/陈述/内容。”。 在接触意见中,我还有部分内容的重复。

意见/内容/新内容

<%= form_for(@contact) do |f| %>
    <div class="field">
        <%= f.label :country %>
        <%= f.collection_select :country_id, @countries, :id, :name, :prompt => "-- Select a country --" %>
    </div>
    <div id="cities" class="field">
        <%= render  cities  %>
    </div>
<% end %>

意见/陈述/内容。

<%= label_tag :city %>
<% unless @cities.blank? %>
    <%= collection_select(:contact, :city_id, @cities, :id, :name, :prompt => true )%>
<% else %>
    <%= select_tag "city_id","city_id", :prompt => "-- Select a city --" %>
<% end %>

然后在您的<>申请中发出Ajax呼吁。

jQuery(function($) {
// when the #region_id field changes
  $("#contact_country_id").live( change , function() {
    // make a POST call and replace the content
    var country = $( select#contact_country_id :selected ).val();
    if(country == "") country="0";
    jQuery.get( /countries/update_city_select/  + country, function(data){
        $("#cities").html(data);
    })
    return false;
  });
})

最后周期是确保你能够从接触形式获得各国的资源。 为此,你刚刚需要在联系资源内为贵国提供资源。 你们还需要要求您提供最新信息,即:

routes.rb
resources :cities
resources :countries
resources :contacts do
  resources :countries
end

get  /countries/update_city_select/:id  =>  countries#update_city_select 

感谢你支持Baldrick和Volodymyr

Aurelien

问题回答

暂无回答




相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

How can i update div continuously

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...