# Collection\_select won't recover from form validation error

**URL:** <https://www.ruby-forum.com/t/collection-select-wont-recover-from-form-validation-error/80463>\
**Category:** Rails\
**Created:** [December 27, 2006, 6:42am UTC](https://www.ruby-forum.com/t/collection-select-wont-recover-from-form-validation-error/80463 "2006-12-27T06:42:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![JL\_Smith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@JL\_Smith](https://www.ruby-forum.com/u/JL_Smith)\
**Post date:** [December 27, 2006, 6:42am UTC](https://www.ruby-forum.com/t/collection-select-wont-recover-from-form-validation-error/80463/1 "2006-12-27T06:42:33Z")

</div>

I can’t quite put my finger on what’s happening here…I’m still trying  
to grasp this framework. With the way my controller methods are  
written, I thought that since I get a form validation error, it would  
kick off the “new” action and gather up those collection\_select  
collections again (although I’m not even sure if that’s the  
problem…nil.inject?). I can add and edit all day but it bombs if I  
have a validation error. Much thanks in advance if anybody can shove me  
in the right direction!

schema:  
CREATE TABLE `tickets` (  
`id` int(11) NOT NULL,  
`title` varchar(255) default NULL,  
`details` text,  
`status_id` int(11) default NULL,  
`priority_id` int(11) default NULL,  
`created_at` datetime default NULL,  
`opened_at` datetime default NULL,  
`closed_at` datetime default NULL,  
`updated_at` datetime default NULL,  
`last_activity_at` datetime default NULL,  
PRIMARY KEY (`id`),  
KEY `fk_ticket_status` (`status_id`),  
KEY `fk_ticket_priority` (`priority_id`),  
CONSTRAINT `fk_ticket_priority` FOREIGN KEY (`priority_id`) REFERENCES  
`ticket_priorities` (`id`),  
CONSTRAINT `fk_ticket_status` FOREIGN KEY (`status_id`) REFERENCES  
`ticket_statuses` (`id`)  
);

CREATE TABLE `ticket_statuses` (  
`id` int(11) NOT NULL,  
`status` varchar(255) default NULL,  
PRIMARY KEY (`id`)  
);

CREATE TABLE `ticket_priorities` (  
`id` int(11) NOT NULL,  
`rank` int(11) default NULL,  
`priority` varchar(255) default NULL,  
PRIMARY KEY (`id`)  
);

Ticket model:  
belongs\_to :ticket\_status, :foreign\_key =\> “status\_id”  
belongs\_to :ticket\_priority, :foreign\_key =\> “priority\_id”  
…  
validates\_presence\_of :title, :details, :status\_id, :priority\_id

controller:  
…  
def new  
@ticket = Ticket.new  
@ticket\_status = TicketStatus.find\_all  
@ticket\_priority = TicketPriority.find\_all  
end

def create  
@ticket = Ticket.new(params[:ticket])  
@ticket.opened\_at = DateTime.now  
@ticket.closed\_at = nil  
@ticket.last\_activity\_at = DateTime.now  
if @ticket.save  
flash[:success] = ‘Ticket was successfully created.’  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
end  
…

error:  
NoMethodError in Ticket#create

Showing app/views/ticket/\_form.rhtml where line #11 raised:  
You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occured while evaluating nil.inject

Extracted source (around line #11):

8: \<%= text\_area ‘ticket’, ‘details’, {:style =\> “width: 325px”} %\>

  
9:  
10: 

Status  
  
11: \<%= collection\_select(“ticket”, “status\_id”, @ticket\_status, “id”,  
“status”, {:prompt =\> “Select status…”}, {:style =\> “width: 150px”})  
%\>

  
12:  
13: 

Priority  
  
14: \<%= collection\_select(“ticket”, “priority\_id”, @ticket\_priority,  
“id”, “priority”, {:prompt =\> “Select priority…”}, {:style =\> “width:  
150px”}) %\>

---

<div class="post-metadata">

**Author:** ![JL\_Smith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@JL\_Smith](https://www.ruby-forum.com/u/JL_Smith)\
**Post date:** [December 28, 2006, 12:58am UTC](https://www.ruby-forum.com/t/collection-select-wont-recover-from-form-validation-error/80463/2 "2006-12-28T00:58:06Z")

</div>

Try changing this:

collection\_select(“ticket”, “priority\_id”, @ticket\_priority,  
“id”, “priority”, {:prompt =\> “Select priority…”}

to this:

collection\_select(@ticket, :priority\_id, @ticket\_priority,  
:id, :priority, {:prompt =\> “Select priority…”}

-Paul

---

<div class="post-metadata">

**Author:** ![JL\_Smith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@JL\_Smith](https://www.ruby-forum.com/u/JL_Smith)\
**Post date:** [December 28, 2006, 3:04am UTC](https://www.ruby-forum.com/t/collection-select-wont-recover-from-form-validation-error/80463/3 "2006-12-28T03:04:33Z")

</div>

Paul C. wrote:

> -Paul
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Hmm…now it won’t even render the form.

NameError in Ticket#new

Showing app/views/ticket/\_form.rhtml where line #11 raised:

`@#Ticket:0xb75ee748’ is not allowed as an instance variable name

Extracted source (around line #11):

8: \<%= text\_area ‘ticket’, ‘details’, {:style =\> “width: 325px”}  
%\>

  
9:  
10: 

Status  
  
11: \<%= collection\_select(@ticket, :status\_id, @ticket\_status, :id,  
:status, {:prompt =\> “Select status…”}, {:style =\> “width: 150px”})  
%\>

  
12:  
13: 

Priority  
  
14: \<%= collection\_select(@ticket, :priority\_id, @ticket\_priority, :id,  
:priority, {:prompt =\> “Select priority…”}, {:style =\> “width:  
150px”}) %\>

---

<div class="post-metadata">

**Author:** ![JL\_Smith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@JL\_Smith](https://www.ruby-forum.com/u/JL_Smith)\
**Post date:** [December 28, 2006, 4:12am UTC](https://www.ruby-forum.com/t/collection-select-wont-recover-from-form-validation-error/80463/4 "2006-12-28T04:12:47Z")

</div>

Well, I found one solution at least. Something must be going on behind  
the scenes that’s not really obvious.

First, I tried this combination:  
controller:  
def new  
@ticket = Ticket.new  
@ticket\_status = TicketStatus.find\_all.collect {|s| [ s.status, s.id  
] }  
@ticket\_priority = TicketPriority.find\_all.collect {|p| [ p.priority,  
p.id ] }  
end

view:

Status  
\<%= select('ticket', 'status\_id', @ticket\_status, {:prompt =\> "Select status..."}, {:style =\> "width: 150px"}) %\>

Priority  
\<%= select('ticket', 'priority\_id', @ticket\_priority, {:prompt =\> "Select priority..."}, {:style =\> "width: 150px"}) %\>

And that still didn’t work. So then I removed both of the collections  
from the controller and did it all in the view like this and it works:

Status  
\<%= select('ticket', 'status\_id', TicketStatus.find\_all.collect {|s| [s.status, s.id] }, { :include\_blank =\> true }, {:style =\> "width: 150px"}) %\>

Priority  
\<%= select('ticket', 'priority\_id', TicketPriority.find\_all.collect {|p| [p.priority, p.id] }, { :include\_blank =\> true }, {:style =\> "width: 150px"}) %\>

I’m also not using the collection\_select anymore but I guess when I  
have a form validation error, it must do it’s own thing somehow and  
clears out those two .find\_all collections. It just doesn’t feel right  
to select data from the view like that…I’d really be interested to  
know what happens when the validation fails and how I could put those  
.find\_all’s in the controller and not the view…
