Hi,
How to i handle a find that returns a null?
Say i put 40 into the paramter id below and there is no post with an id
of 40
i get an error saying no post exists with id
@post = Post.find(params[:id])
Anyone help??
Hi,
How to i handle a find that returns a null?
Say i put 40 into the paramter id below and there is no post with an id
of 40
i get an error saying no post exists with id
@post = Post.find(params[:id])
Anyone help??
That won’t work either. Try one of these:
begin
@post = Post.find(params[:id])
rescue
end
#-OR-
if @post = Post.find_by_id(params[:id])
else
end
The dynamic finders (find_by_id in this case) return nil, but find
raises an exception.
-Rob
Rob B. http://agileconsultingllc.com
[email protected]
+1 513-295-4739
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs