wolfgang
2009-Jan-11 00:27 UTC
Got an error on returning data as json format when the data is nil
Hi there, One of my app''s actions returns data as a json format. If the data exists, everything works perfect. But if the data is nil, I get the following error. -------------------------------- ActionView::MissingTemplate (Missing template user/show.erb in view path /var/www/my_app/releases/20090110234348/app/views:): /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/ template.rb:95:in `find_full_path'' /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/ template.rb:16:in `initialize'' /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/ base.rb:332:in `new'' /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/ base.rb:332:in `_unmemoized__pick_template'' /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/ active_support/memoizable.rb:70:in `_pick_template'' -------------------------------- I don''t know why rails requires a template file only when the data is nil. Anybody knows the reason ? Here is a snippet from the controller. --------------------------------------------------- class UserController < ApplicationController def show name = params[:name] person = User.find_by_name(name) respond_to do |format| format.html # show.html.erb format.json { render :json => person } end end end ----------------------------------------------------- How can I avoid this situation ??? Thanks in advance. -wolfgang --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Robert Walker
2009-Jan-11 23:39 UTC
Re: Got an error on returning data as json format when the d
wolfgang wrote:> How can I avoid this situation ???I don''t know specifically why Rails generates that particular error, but that doesn''t really matter, because render :json => nil is not valid. You need to check for nil and respond with a properly constructed error response, including the proper HTTP error status. I think most people response with a 404 - Page not found in this case, but I''m not completely sure of that. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
wolfgang
2009-Jan-13 21:06 UTC
Re: Got an error on returning data as json format when the d
Hi Robert, Thanks for reply. So does it mean JSON cannot represent a single nil object ? serious !? Ajax calls the action and it can treat the error as nil object as you mentioned but isn''t it ugly ??? thanks in advance. wolfgang On Mon, Jan 12, 2009 at 8:39 AM, Robert Walker <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > wolfgang wrote: >> How can I avoid this situation ??? > I don''t know specifically why Rails generates that particular error, but > that doesn''t really matter, because render :json => nil is not valid. > You need to check for nil and respond with a properly constructed error > response, including the proper HTTP error status. I think most people > response with a 404 - Page not found in this case, but I''m not > completely sure of that. > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Maybe Matching Threads
- Rails3: render_to_string of a .html.erb when request is a json request raises MissingTemplate error
- rake aborted! undefined method `empty?' for nil:NilClass
- Problem running rake test
- Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
- nil object while the required parameter are in the request