views:

39

answers:

1

Here's the output:

 Parameters: {"action"=>"confirm", "id"=>"1", "controller"=>"sites"}
 User Columns (2.2ms)   SHOW FIELDS FROM `users`
 User Load (0.3ms)   SELECT * FROM `users` WHERE (`users`.`id` = 2) LIMIT 1
 School Load (0.3ms)   SELECT * FROM `schools` LIMIT 1
 Rendering template within layouts/application
 Rendering sites/confirm

ActionView::TemplateError (You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.map) on line #4 of app/views/sites/confirm.erb:
 1: 
 2:  <%   
 3:    form_for(:site, :url => {:action => :confirm}) do 
 4:    select_tag(:id, options_from_collection_for_select(@confirm, 'id', 'confirm')) 
 5:    end
 6:   %>

Even when I did: @confirm = Request.find(:all) in controller, it still returned the error.

Sites Controller:

def confirm
x = current_user.contact.contactable
@confirm = Request.find(:all, :conditions => ["location_id = ?", x])
end

Confirm view in sites folder:

<%   
form_for(:site, :url => {:action => :confirm}) do 
select_tag(:id, options_from_collection_for_select(@confirm, 'id', 'confirm')) 
end
%>

Any idea why? Error occurs on line 4. I believe it thinks @confirm is not an array, although it should be because in controller I pass two items from table into it. I made the options_from_collection_for_select options similar to the one in the rails guide. Basically, I want to show a list of options to select from based on whether those options match the location of the current user. Thanks for any suggestions.

+1  A: 

The error is implying that the @confirm instance variable within your controller's confirm method is nil. Can you add some logging to verify if this is the case or not?


Edit: I've just noticed that your form_for block as posted in the question uses:

<%= form_for ... %>

When it should be:

<% form_for ... %>
John Topley
I updated the question with details from output. Even when I did a find(:all), and despite the fact that I have multiple records in database and I checked that it realized them in script/console, it still returns a nil object in rails.
JohnMerlino
Shouldn't you be using `select` rather than `select_tag` with `form_for` because it's for working with model objects.
John Topley
Thanks. It's not recognizing the instance variable at all, even when I assign it an obvious array in the controller like Students.all. It doesn't recognize it in the view. Am I missing something in the view for the instance variable to be recognized?
JohnMerlino
I moved the button to the request controller, and the instance variable would work in the view for that controller. It just wouldn't work in the site controller and site view. I assume it's because I performed a find on request from site, but I'm sure there's a solution out there to make it work.
JohnMerlino
Sorry, there's not really enough to go on from the code you've posted to see where the problem is.
John Topley