I’m having some trouble with an instance variable being read as nil in
my app. I have an image uploading, that can be added to categories.
The page loads up initially fine, and if I upload an image without
errors everything works fine. I’m working on the validation, but with
the code I have if their are validation errors it renders ‘new’ again,
but reads @categories as nil and errors out.
In render :action => ‘new’, does it just render the template or should I
have the instance variable as well also? I’ve tried changing it to
redirect_to and it doesn’t error, but also doesn’t provide the error
messages I want.
I’ve got this in my ImageController:
@categories = Category.find(:all)
@image = Image.new(params[:image])
@image.categories = Category.find(params[:category_ids]) if
redirect_to :action => ‘list’
flash[:notice] = ‘Image was successfully uploaded.’
render :action => ‘new’
And this code in the view gets the error:
Add Image to the Following Categories<% for category in @categories %> <%=category.name%>
<% end %>
And the error:
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.each
Extracted source (around line #21):
Add Image to the Following Categories
21: <% for category in @categories %>