Forum: Ruby on Rails instance variable problem

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E25296bf52723dc37bc462271eb02d2d?d=identicon&s=25 Jason Pfeifer (jpfeifer)
on 2006-06-01 01:19
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:

def new
    @categories = Category.find(:all)
  end

  def create
    if request.post?
      @image = Image.new(params[:image])
      @image.categories = Category.find(params[:category_ids]) if
params[:category_ids]
      if @image.save
         redirect_to :action => 'list'
         flash[:notice] = 'Image was successfully uploaded.'
      else
        render :action => 'new'
      end
    end
  end

And this code in the view gets the error:

<div class="admin_content_row">
  <h2>Add Image to the Following Categories</h2>
  <% for category in @categories %>
    <input type="checkbox" id="<%= category.id %>" name="category_ids[]"
value="<%=category.id%>" />
    &nbsp;<%=category.name%><br />
  <% end %>
</div>

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):

18:
19: <div class="admin_content_row">
20:   <h2>Add Image to the Following Categories</h2>
21:   <% for category in @categories %>
C9c7a65848f13e2b1a226bbe43bc3672?d=identicon&s=25 Pete Yandell (pete)
on 2006-06-01 03:39
(Received via mailing list)
Jason,

When you call render :action => 'new' from your create action,
there's no @categories variable set. You either need to do:

   @categories = Category.find(:all)
   render :action => 'new'

or, probably better:

   new
   render :action => 'new'

Pete Yandell
http://9cays.com/
E25296bf52723dc37bc462271eb02d2d?d=identicon&s=25 Jason Pfeifer (jpfeifer)
on 2006-06-01 05:05
Pete Yandell wrote:
> Jason,
>
> When you call render :action => 'new' from your create action,
> there's no @categories variable set. You either need to do:
>
>    @categories = Category.find(:all)
>    render :action => 'new'
>
> or, probably better:
>
>    new
>    render :action => 'new'
>
> Pete Yandell
> http://9cays.com/

Cheers,

I guess what confused me is I thought render :action => 'new' was a call
to the function.  But it's just rendering the template I see...
Ed5307c79ba5b415b1ddfee48319099b?d=identicon&s=25 Ioana Kanda (Guest)
on 2006-06-01 10:13
(Received via mailing list)
Try doing this: "       render :action => 'new', :locals => {:categories
=>
@categories}        " , where @categories = Category.find(:all) or what
u
want.
In your view u can access categories in this way: "    for category in
categories     ".
Good luck...hope it works
This topic is locked and can not be replied to.