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:
-
How to get this lovely parametrs in update method?
-
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:
- 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 -
@account = Account.find(params[:id])
-
mama = params[:city_id] # here it is null.
-
respond_to do |format|
-
if @account.update_attributes(params[:account])
-
flash[:notice] = 'Account was successfully updated.'
-
format.html { redirect_to(@account) }
-
format.xml { head :ok }
-
else
-
format.html { render :action => "edit" }
-
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:
-
Editing account
- <%= error_messages_for :account %>
- <% form_for(@account) do |f| %>
-
-
<b>Name1</b><br />
-
<%= f.text_field :name1 %>
-
-
<b>Country</b><br />
-
<%= collection_select(nil, :country_id, @countries, :id,
:name,{},
14. {:onchange => ā#{remote_function(:url => {:action =>
āupdate_citiesā},
15. :with =>
āācountry_id=ā+valueā)}ā}) %>
16.
17.
18.
@cities %>
19.
20.
21. Avatar
22. <%= f.text_field :avatar %>
23.
24.
25.
26. <%= f.submit āUpdateā %>
27.
28. <% end %>
partial _city:
-
-
<b>City</b><br />
-
<%= collection_select(nil, :city_id, cities, :id, :name) %>