I am using: if @post.empty? render_text "Post does not exist." end But it gives me this error: NoMethodError in Posts#view undefined method `empty?'' for #<Posts:0x39598d0> However, if the method list empty? apears and stuff so I am confused. -- Posted via http://www.ruby-forum.com/.
It looks like you are calling it on a Post object, where the .empty is meant for collections/strings and such. Should the above line read, if @posts.empty? .. Notice the s. I''m guessing you are loading posts like: @posts = Post.find(:all)? if so then @posts.empty? should work just fine. -Nick On 2/11/06, Michael Boutros <me@michaelboutros.com> wrote:> I am using: > > if @post.empty? > render_text "Post does not exist." > end > > But it gives me this error: > > NoMethodError in Posts#view > > undefined method `empty?'' for #<Posts:0x39598d0> > > However, if the method list empty? apears and stuff so I am confused. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Nick Stuart wrote:> It looks like you are calling it on a Post object, where the .empty is > meant for collections/strings and such. Should the above line read, > if @posts.empty? > .. > > Notice the s. I''m guessing you are loading posts like: > @posts = Post.find(:all)? > > if so then @posts.empty? should work just fine. > > -NickNo, I am doing a view page to view an entry seperately. I am using the .empty? method to check if the row exists, and if it does not I will output an error. This is the entire page code: class PostsController < ApplicationController def index @posts = Posts.find_all end def view @post = Posts.find(@params["id"]) if @post.empty? render_text "Post does not exist." end end end -- Posted via http://www.ruby-forum.com/.
Michael Boutros wrote:> def view > @post = Posts.find(@params["id"]) > if @post.empty? > render_text "Post does not exist." > end > endYou can''t use empty? on a class object. That method isn''t supported, which i believe is exactly what the error message tried to tell you. You want to check to see if the result of the find command has resulted in a defined @post object. Change the condition to: if @post.nil? or: unless @post -Brian