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
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>