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%>" /> <%=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 %> -- Posted via http://www.ruby-forum.com/.
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/ On 01/06/2006, at 9:19 AM, Jason Pfeifer wrote:> 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%>" /> > <%=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 %> > > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
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... -- Posted via http://www.ruby-forum.com/.
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 On 6/1/06, Jason Pfeifer <jpfeifer@shaw.ca> wrote:> > 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%>" /> > <%=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 %> > > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Cheers, ioana k&a http://boulangerie.wordpress.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060601/6c00c322/attachment.html