# Collection\_select fails to work for "edit" but works great for "new"

**URL:** <https://www.ruby-forum.com/t/collection-select-fails-to-work-for-edit-but-works-great-for-new/177577>\
**Category:** Rails\
**Created:** [October 20, 2009, 2:08pm UTC](https://www.ruby-forum.com/t/collection-select-fails-to-work-for-edit-but-works-great-for-new/177577 "2009-10-20T14:08:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ct9a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@ct9a](https://www.ruby-forum.com/u/ct9a)\
**Post date:** [October 20, 2009, 2:08pm UTC](https://www.ruby-forum.com/t/collection-select-fails-to-work-for-edit-but-works-great-for-new/177577/1 "2009-10-20T14:08:49Z")

</div>

Hi, all,

I setup a drop down box that has data populated by the database.

This is for a simple application where by I am trying to add a new  
“Part”.  
A part has a category and sub category.  
The latter is optional.

When a category has been selected, an ajax query is made to the  
database backend to  
extract the subcategory options available for the selected category.

This has been implemented and works great for the task of adding a new  
“Part” entry (ie. [http://localhost:3000/parts/new](http://localhost:3000/parts/new)) .

I did it by refering to the explanation in  
[http://shiningthrough.co.uk/blog/show/6](http://shiningthrough.co.uk/blog/show/6).  
It was good.

Nevertheless, when I need to edit a given part (ie.  
[http://localhost:3000/parts/22/edit](http://localhost:3000/parts/22/edit)),  
I keep getting this silly error message below.  
“undefined method `map’ for “#Category:0x103231d20”:Category”.

This is the full extract of the error messages:

============Extract Begin===============================  
NoMethodError in Parts#edit

Showing app/views/categories/\_form.html.erb where line #3 raised:

undefined method `map’ for “#Category:0x1036113c8”:Category

Extracted source (around line #3):

1: \<%= label :category, :id, ‘Category’ %\>   
  
2: \<%= collection\_select(  
3: :part,  
4: ‘category\_id’,  
5: @categories,  
6: :id,

Trace of template inclusion: app/views/parts/edit.html.erb  
============Extract End===============================

Here’s what my other files look like:

## app/views/parts/new.html.erb

1

# Add a new part
  
2  
3 \<% form\_for(@part, @make, @model) do |f| %\>  
4 \<%= f.error\_messages %\>  
5  
6 

7 \<%= f.label :title %\>  
  
8 \<%= f.text\_field :title %\>  
9

  
10

11   
12 \<%= render :partial =\> ‘categories/form’,  
13 :locals =\> {:category =\> @categories} %\>  
14  
15   
16   
17  
18

19 \<%= observe\_field :part\_category\_id,  
20 :url =\> { :action =\> :get\_subcategories },  
21 :update =\> :subcategory\_div,  
22 :with =\> “‘category=’ + value”  
23 %\>  
24

…

## app/views/parts/new.html.erb

1

# Editing part
  
2  
3 \<% form\_for(@part) do |f| %\>  
4 \<%= f.error\_messages %\>  
5  
6 

7 \<%= f.label :title %\>  
  
8 \<%= f.text\_field :title %\>  
9

  
10  
11   
12 \<%= render :partial =\> ‘categories/form’,  
13 :locals =\> {:category =\> @categories} %\>  
14  
15   
16   
17  
18 

19 \<%= observe\_field :part\_category\_id,  
20 :url =\> { :action =\> :get\_subcategories },  
21 :update =\> :subcategory\_div,  
22 :with =\> “‘category=’ + value”  
23 %\>  
24

   
25  
26 

27 \<%= f.submit ‘Update’ %\>  
28

   
29 \<% end %\>  
…

 
## app/views/categories/\_form.html.erb
 

1 \<%= label :part, ‘category\_id’, ‘Category’ %\>   
  
2 \<%= collection\_select(  
3 :part,  
4 ‘category\_id’,  
5 @categories,  
6 :id,  
7 “name”,  
8 {:include\_blank =\> true}  
9 )  
10 %\>

 

Any ideas?

 
# What I have referenced:
 

> **[Action View Form Helpers — Ruby on Rails Guides](https://guides.rubyonrails.org/form_helpers.html)**
>
> Action View Form HelpersForms in web applications are an essential interface for user input. However, form markup can quickly become tedious to write and maintain because of the need to handle form control naming and its numerous attributes. Rails...

 

[http://shiningthrough.co.uk/blog/show/6](http://shiningthrough.co.uk/blog/show/6)  
[http://www.kahfei.com/?p=23](http://www.kahfei.com/?p=23)

---

<div class="post-metadata">

**Author:** ![ct9a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@ct9a](https://www.ruby-forum.com/u/ct9a)\
**Post date:** [October 20, 2009, 2:53pm UTC](https://www.ruby-forum.com/t/collection-select-fails-to-work-for-edit-but-works-great-for-new/177577/2 "2009-10-20T14:53:20Z")

</div>

and by the way, the parts controller looks the following:

def new  
@part = Part.new  
@categories = Category.all

```
respond_to do |format|
  format.html # new.html.erb
  format.xml { render :xml => @part }
end

```

end

# GET /parts/1/edit

def edit  
@part = Part.find(params[:id])  
@categories = @part.category

```
respond_to do |format|
  format.html # edit.html.erb
  format.xml { render :xml => @part }
end

```

end

---

<div class="post-metadata">

**Author:** ![ct9a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@ct9a](https://www.ruby-forum.com/u/ct9a)\
**Post date:** [October 20, 2009, 3:06pm UTC](https://www.ruby-forum.com/t/collection-select-fails-to-work-for-edit-but-works-great-for-new/177577/3 "2009-10-20T15:06:47Z")

</div>

You don’t show your Part model, but I suspect that it looks something  
like:

class Part \< ActiveRecord::Base  
belongs\_to :category  
end

This allows you to do something like:

@part = Part.find(params[:id])  
@category = @part.category

Note the subtle difference between what I wrote and what you wrote in  
your edit view: (@category vs @categories)

A part belongs to a single category. When you reference  
@part.category, you get that single category – the category to which  
the part belongs. In particular, you don’t get a list of categories.  
Since you don’t have a list of categories, you don’t have a  
“collection” to which you can pass to #collection\_select.

If you want to be able to use the Edit view to change the category to  
which a part belongs, then you should probably do the same thing you  
did in the action for your New view and do:

@categories = Category.all

–wpd

---

<div class="post-metadata">

**Author:** ![ct9a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@ct9a](https://www.ruby-forum.com/u/ct9a)\
**Post date:** [October 20, 2009, 3:08pm UTC](https://www.ruby-forum.com/t/collection-select-fails-to-work-for-edit-but-works-great-for-new/177577/4 "2009-10-20T15:08:33Z")

</div>

2009/10/20 ct9a [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Â Â Â format.xml Â { render :xml =\> @part }  
> Â Â end  
> Â end
> 
> Â # GET /parts/1/edit  
> Â def edit  
> Â Â @part = Part.find(params[:id])  
> Â Â @categories = @part.category

That makes @categories only be a single category, which I think may be  
the cause of the problem.

Colin

---

<div class="post-metadata">

**Author:** ![ct9a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@ct9a](https://www.ruby-forum.com/u/ct9a)\
**Post date:** [October 20, 2009, 3:12pm UTC](https://www.ruby-forum.com/t/collection-select-fails-to-work-for-edit-but-works-great-for-new/177577/5 "2009-10-20T15:12:39Z")

</div>

aha! fixed it.

1. Removed usage of @categories in the collection\_select call in the  
parts/new and edit views. Replaced it with ‘Category.all’
2. Removed population of @categories by means of Categories.all in  
the parts controller.
3. Removed putting in of ‘local’ hashref of :categories =\> @categories  
in the category/form partial (  
app/views/categories/\_form.html.erb ).

---

<div class="post-metadata">

**Author:** ![ct9a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@ct9a](https://www.ruby-forum.com/u/ct9a)\
**Post date:** [October 22, 2009, 1:27am UTC](https://www.ruby-forum.com/t/collection-select-fails-to-work-for-edit-but-works-great-for-new/177577/6 "2009-10-22T01:27:08Z")

</div>

ah, yes, as in doing so in new & edit methods in the Parts controller?

---

<div class="post-metadata">

**Author:** ![ct9a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@ct9a](https://www.ruby-forum.com/u/ct9a)\
**Post date:** [October 20, 2009, 6:40pm UTC](https://www.ruby-forum.com/t/collection-select-fails-to-work-for-edit-but-works-great-for-new/177577/7 "2009-10-20T18:40:43Z")

</div>

A bit heavy-handed…

> 1. Removed usage of @categories in the collection\_select call in the  
> parts/new and edit views. Replaced it with ‘Category.all’

Now those new and edit views will always show all categories…  
determining WHAT will be shown isn’t usually the purview of the view.

> 1. Removed population of @categories by means of Categories.all in  
> the parts controller.

But sometimes the controller determines what should be shown… it may  
be nice to have one hunk of view code that can deal with different  
conditions, i.e., maybe some categories, but not all… adopt that whole  
Model-View-Controller thing.

> 1. Removed putting in of ‘local’ hashref of :categories =\> @categories  
> in the category/form partial (  
> app/views/categories/\_form.html.erb ).

That’s just plumbing dependent on where the categories are sourced…

The simpler solution was just to instantiate @categories similarly for  
both the new and edit methods.
