# Problems with collection\_selects

**URL:** <https://www.ruby-forum.com/t/problems-with-collection-selects/148995>\
**Category:** Rails\
**Created:** [September 19, 2008, 4:02am UTC](https://www.ruby-forum.com/t/problems-with-collection-selects/148995 "2008-09-19T04:02:30Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![maep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90ced4/32.png) [@maep](https://www.ruby-forum.com/u/maep)\
**Post date:** [September 19, 2008, 4:02am UTC](https://www.ruby-forum.com/t/problems-with-collection-selects/148995/1 "2008-09-19T04:02:30Z")

</div>

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](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
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. 
3. \<%= error\_messages\_for :account %\>
4. 
5. \<% form\_for(@account) do |f| %\>
6. 

7. 

```
<b>Name1</b><br />

```

8. 

```
<%= f.text_field :name1 %>

```

9. 

10. 
11. 

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.

  
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. 

2. 

```
<b>City</b><br />

```

3. 

```
<%= collection_select(nil, :city_id, cities, :id, :name) %>

```

4.
