title: Posting json requests to Ruby on Rails server app – 500 error
body: <p>I have a Ruby on Rails server application(hosted on Heroku) which is supposed to be able to receive HTTP POST requests with JSON strings and add the JSON objects to the database. There are two database models: thanksgivings and requests.</p>
  
  <p>controller/request_controller.rb:</p>
  
  <pre><code>class RequestsController &lt; ApplicationController
    [...]
    # POST /requests
    # POST /requests.json
    def create
      @request = Request.new(params[:request])
  
      respond_to do |format|
        if @request.save
          format.html { redirect_to @request, notice: 'Request was successfully created.' }
          format.json { render json: @request, status: :created, location: @request }
        else
          format.html { render action: "new" }
          format.json { render json: @request.errors, status: :unprocessable_entity }
        end
      end
    end
  [...]
  end
  </code></pre>
  
  <p>The JSON keys for the different database model objects values should be "thanks" and "request" for the Thankgiving DB model and Request DB model respectively.</p>
  
  <p>model/request.rb:</p>
  
  <pre><code>class Request &lt; ActiveRecord::Base
    attr_accessible :request
  end
  </code></pre>
  
  <p>model/thankgiving.rb:</p>
  
  <pre><code>class Thanksgiving &lt; ActiveRecord::Base
    attr_accessible :thanks
  end
  </code></pre>
  
  <p>When I post an HTTP request to <a href="http://YADA.herokuapp.com/thanksgivings.json" rel="nofollow">http://YADA.herokuapp.com/thanksgivings.json</a> everything works fine, but when I send it to .../requests.json, I get a 500 error. If I use a faulty key in the JSON string, the request succeeds, but the value becomes null. </p>
  
  <p>Request 1: (correct request to thanksgiving.json)<br>
  POST<br>
  Host: <a href="http://YADA.herokuapp.com/thanksgivings.json" rel="nofollow">http://YADA.herokuapp.com/thanksgivings.json</a><br>
  Content-Type: application/json<br>
  Body: {"thanks":"qwerty"}  </p>
  
  <p>Request 2: (correct request to request.json – returns 500)<br>
  POST<br>
  Host: <a href="http://YADA.herokuapp.com/requests.json" rel="nofollow">http://YADA.herokuapp.com/requests.json</a><br>
  Content-Type: application/json<br>
  Body: {"request":"qwerty"}   </p>
  
  <p>Request 3: (faulty key - will succeed: value will be null, key will be "request" in response, database element will be added)<br>
  POST<br>
  Host: <a href="http://YADA.herokuapp.com/requests.json" rel="nofollow">http://YADA.herokuapp.com/requests.json</a><br>
  Content-Type: application/json<br>
  Body: {"abc":"qwerty"} </p>
  
  <p>The strange thing is that the two controllers, thanksgivings_controller.rb and requests_controller.rb, are "identical" but their behavior differs.</p>
  
  <p>Anyone knows how to do this successfully?</p>