title: Rails 3: Why might my respond_to statement throw this exception when called from a POST request?
body: <pre><code>respond_to :json, :html
  .
  .
  .
  return_hash = {}
  return_hash[:result] = "valid"
  return_hash[:status] = "#{userName} has successfully registered for tournament #{tourneyID}"
  respond_with(return_hash) #&lt;--Throwing expection NoMethodError (undefined method `model_name' for NilClass:Class):
  </code></pre>
  
  <p>Here's the stack trace...</p>
  
  <pre><code>NoMethodError (undefined method `model_name' for NilClass:Class):
  app/controllers/tournaments_controller.rb:48:in `register'
  
  Rendered /Users/myname/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.5/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.8ms)
  Rendered /Users/myname/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.5/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (151.8ms)
  Rendered /Users/myname/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.5/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (211.1ms)
  </code></pre>
  
  <p>Thanks so much!</p>
  
  <p>Not sure if it matters, but I should add this is called in response to a POST request</p>
  
  <p><em><strong>Update:</em></strong> 
  I have similar code that works fine, it looks like this...</p>
  
  <pre><code> # Store the tourney data
   tourney_hash[:tournament_count] = 1
   tourney_hash[:tournament_id] = nextTourney.id
   tourney_hash[:remaining_time_in_seconds] = remainingTimeInSeconds   
   respond_with(tourney_hash)
  </code></pre>
  
  <p>The only difference is this code is called from a GET request, while the problematic code above is called from a POST request</p>
  
  <p><em><strong>UPDATE:</em></strong>
  When I changed this code so that it's called from a GET request instead of a POST request, it works fine.  Your thoughts?</p>
  
  <p><em><strong>UPDATE:</em></strong>
  For the time being, I've replaced the statement "respond_with(return_hash)" with "render :json => return_hash.to_json" and it works fine.  Not ideal, tho.</p>