Problems with collection_selects

Hello friends!
I am trying to make city select on my page. So, I have countries, that
has_many cities. account belongs_to city.

I have found example here:
http://pullmonkey.com/2008/3/30/dynamic-select-boxes-ruby-on-rails

So, this way I am doing that. But I have one problem, and two questions
:slight_smile:

Problem: when user submits form (press Update button), I need do assign
@account.city_id to chosen city_id. I canā€™t get value of parametr! (Even
country_id seems to be nil in update method)

Questions:

  1. How to get this lovely parametrs in update method?

  2. How to use form_for.collection_select helper? I am using form_for,
    and itā€™s rather strange not to use collection_select from it, but it
    does not work!
    And, finally - how to make collection_Select result be avaliable in
    account parmetrs, to use just @account.update_attributes (without
    rewriting update method)

Thank you in advance,
here is the code:

account controller:

  1. class AccountsController < ApplicationController
    2. ā€¦
    3. # GET /accounts/1/edit
    4. def edit
    5. @account = Account.find(params[:id])
    6. @countries = Country.find(:all)
    7. @cities = City.find(:all)
    8. end
    9. def update
  2. @account = Account.find(params[:id])
    
  3. mama = params[:city_id] # here it is null.
    
  4. respond_to do |format|
    
  5.   if @account.update_attributes(params[:account])
    
  6.     flash[:notice] = 'Account was successfully updated.'
    
  7.     format.html { redirect_to(@account) }
    
  8.     format.xml  { head :ok }
    
  9.   else
    
  10.     format.html { render :action => "edit" }
    
  11.     format.xml  { render :xml => @account.errors, :status =>
    

:unprocessable_entity }
20. end
21. end
22. end
23. def update_cities
24. puts "mama papa " + params[:country_id].to_s
25. country = Country.find(params[:country_id])
26. cities = country.cities
27.
28. render :update do |page|
29. page.replace_html ā€˜citiesā€™, :partial => ā€˜citiesā€™, :object =>
cities
30. end
31.
32. end
33. end

edit view:

  1. Editing account

  2. <%= error_messages_for :account %>
  3. <% form_for(@account) do |f| %>
  4. <b>Name1</b><br />
    
  5. <%= f.text_field :name1 %>
    
  6. <b>Country</b><br />
    
  7. <%= collection_select(nil, :country_id, @countries, :id,
    

:name,{},
14. {:onchange => ā€œ#{remote_function(:url => {:action =>
ā€œupdate_citiesā€},
15. :with =>
ā€œā€˜country_id=ā€™+valueā€)}ā€}) %>
16.


17.
18.
<%= render :partial => ā€œcitiesā€, :object =>
@cities %>

19.
20.


21. Avatar

22. <%= f.text_field :avatar %>
23.


24.
25.


26. <%= f.submit ā€œUpdateā€ %>
27.


28. <% end %>

partial _city:

  1. <b>City</b><br />
    
  2. <%= collection_select(nil, :city_id, cities, :id, :name) %>