Reputation: 239
``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 %>
please there seem to be somthing wrong with the observe_field, what i was expecting it to do was when i put in a value for country, the states under that country shows up
Upvotes: 0
Views: 2087
Reputation: 13026
add the following to your Gemfile and run bundle:
gem 'prototype_legacy_helper', '0.0.0', :git => 'git://github.com/rails/prototype_legacy_helper.git'
gem 'prototype-rails'
Upvotes: 0
Reputation: 834
It looks like you made an error where you embed the Ruby: <% observe_field %>
inserts nothing, while <%= observe_field %>
inserts the output from the function.
Try this:
<%= observe_field 'person_country_id', :url => {:action => "update_state_div"}, :with => 'person_country_id'%>
Edit:
observe_field
was included to supplement Prototype, a javascript library not included in Ruby on Rails 3. This question also addresses this issue: Observe_field in rails 3.
If you want to use it, you can install this plugin by typing
rails plugin install git://github.com/rails/prototype_legacy_helper
into the console.
This seems to not be compatible with jQuery, so you might have to remove jQuery, or write your own javascript to do what you want.
Yet another edit:
This question addresses the issue in normal JavaScript/jQuery and HTML: jQuery Chaining Country / State Selects with OptGroup. You should be able to modify it for Ruby on Rails.
Upvotes: 3