collection_select won't recover from form validation err

Paul Corcoran wrote:

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

--
Posted via 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"}
%></p>
9:
10: <p><label for="ticket[status_id]">Status</label><br/>
11: <%= collection_select(@ticket, :status_id, @ticket_status, :id,
:status, {:prompt => "Select status..."}, {:style => "width: 150px"})
%></p>
12:
13: <p><label for="ticket[priority_id]">Priority</label><br/>
14: <%= collection_select(@ticket, :priority_id, @ticket_priority, :id,
:priority, {:prompt => "Select priority..."}, {:style => "width:
150px"}) %></p>

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:
<p><label for="ticket[status_id]">Status</label><br/>
<%= select('ticket', 'status_id', @ticket_status, {:prompt => "Select
status..."}, {:style => "width: 150px"}) %></p>

<p><label for="ticket[priority_id]">Priority</label><br/>
<%= select('ticket', 'priority_id', @ticket_priority, {:prompt =>
"Select priority..."}, {:style => "width: 150px"}) %></p>

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:
<p><label for="ticket[status_id]">Status</label><br/>
<%= select('ticket', 'status_id', TicketStatus.find_all.collect {|s| [
s.status, s.id ] }, { :include_blank => true }, {:style => "width:
150px"}) %></p>

<p><label for="ticket[priority_id]">Priority</label><br/>
<%= select('ticket', 'priority_id', TicketPriority.find_all.collect
{|p| [ p.priority, p.id ] }, { :include_blank => true }, {:style =>
"width: 150px"}) %></p>

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