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   10. @account = Account.find(params[:id])   11. mama = params[:city_id] # here it is null.   12. respond_to do |format|   13. if @account.update_attributes(params[:account])   14. flash[:notice] = 'Account was successfully updated.'   15. format.html { redirect_to(@account) }   16. format.xml { head :ok }   17. else   18. format.html { render :action => "edit" }   19. 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. <h1>Editing account</h1>    2.    3. <%= error_messages_for :account %>    4.    5. <% form_for(@account) do |f| %>    6. <p>    7. <b>Name1</b><br />    8. <%= f.text_field :name1 %>    9. </p>   10.   11. <p>   12. <b>Country</b><br />   13. <%= collection_select(nil, :country_id, @countries, :id, :name,{},   14. {:onchange => "#{remote_function(:url => {:action => "update_cities"},   15. :with => "'country_id='+value")}"}) %>   16. </p>   17.   18. <div id = "cities"><%= render :partial => "cities", :object => @cities %> </div>   19.   20. <p>   21. <b>Avatar</b><br />   22. <%= f.text_field :avatar %>   23. </p>   24.   25. <p>   26. <%= f.submit "Update" %>   27. </p>   28. <% end %>

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