2011-12-04 19 views
0
``enter code here<%= form_for(@person) do |f| %> 
    <% if @person.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@person.errors.count, "error") %> prohibited this person from being saved:</h2> 

     <ul> 
     <% @person.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <div class="field"> 
    <%= f.label :name %><br /> 
    <%= f.text_field :name %> 
    </div> 
    <div class="field"> 
    <%= f.label :country_id %><br /> 
    <%= f.collection_select :country_id, Country.all(:order => "name"), :id, :name, :prompt => "-- Select a Country --"%> 
    <%= observe_field (:person_country_id, :url => {:action => "update_state_div"}, :with => 'person_country_id'%> 
    </div> 
    <div class="field"> 
    <%= f.label :state_id %><br /> 
    <%= f.collection_select :state_id, State.all(:order => "name"), :id, :name, :prompt => "-- Select a State --"%> 


    <%observe_field 'person_country_id', :url => {:action => "update_state_div"}, :with => 'person_country_id'%> 
    </div> 
    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

取悅似乎那裏不會停錯了的observe_field,我期待它做的是,當我把在國內的值,該國下的狀態顯示出來的observe_field錯誤

回答

3

它看起來像你在嵌入Ruby時出錯:<% observe_field %>什麼都不插入,而<%= observe_field %>插入函數的輸出。

試試這個:

<%= observe_field 'person_country_id', :url => {:action => "update_state_div"}, :with => 'person_country_id'%> 

編輯:

observe_field被列入補充原型,一個on Rails的3 JavaScript庫not included在Ruby中這個問題也解決了這個問題:Observe_field in rails 3

如果你想使用它,你可以通過鍵入

rails plugin install git://github.com/rails/prototype_legacy_helper 

到控制檯安裝this plugin

這似乎與jQuery不兼容,因此您可能必須刪除jQuery,或編寫自己的JavaScript來執行所需操作。

另一個編輯:

這個問題在普通的JavaScript/jQuery的解決了上述問題和HTML:jQuery Chaining Country/State Selects with OptGroup。你應該可以修改它在Ruby on Rails上。

+0

啊。 'observe_field'不再包含在Rails中 - 請參閱我的編輯信息。 – ThatOtherPerson

0

以下內容添加到您的Gemfile和運行包:

gem 'prototype_legacy_helper', '0.0.0', :git => 'git://github.com/rails/prototype_legacy_helper.git' 
gem 'prototype-rails' 
相關問題