title: Fetching Post data from REST in Rails
body: <p>This is a long question so i apologize if it takes a while to grasp. I'll keep it simple as possible.</p>
  
  <p>I'm starting to get a little desperate here but I'm so close. I've been struggling with Ruby for a while and communicating with post and net::http. So what I've got at the moment is a hash being turned into json. sent across the wire using a post request from net::http and then I'm trying to turn it back into an object.</p>
  
  <p>One route I explored was active record and active resource but after some issues with them on both sides decided to define my own model and then work out how to convert is and send it.</p>
  
  <h3>My Model</h3>
  
  <p>This model is called customRequest. Within it I have the post_me that takes 2 of the classes variables into a hash, turns it to json, then sends it.</p>
  
  <pre><code>class CustomRequest
    require 'json'
    require 'net/http'
    attr_accessor :myString, :myInteger
  
    def initialize(myString,myInteger)
      @myString = myString
      @myInteger = myInteger
    end
  
    def post_me
  
      @myReq = {:myString =&gt; @myString, :myInteger =&gt;@myInteger}
      myJsonReq = @myReq.to_json
      #myJsonReq = JSON.generate(@myReq)
      puts myJsonReq
      #    res = Net::HTTP.post_form(URI.parse('http://127.0.0.1:3008/user_requests/add'),
      #    myJsonReq)
  
      res = Net::HTTP.post_form(URI.parse('http://127.0.0.1:3008/user_requests/add.json'),
      myJsonReq).as_json
    end
  end
  </code></pre>
  
  <p>So thats the custom request in a nutshell.</p>
  
  <h3>Questions</h3>
  
  <p>The bits I'm concerned about are:</p>
  
  <ol>
  <li>The receiving URL, I wrote add.json as the format is determined in the controller route. I don't know if that's necessary?</li>
  <li>Is it better to generate a hash from the variables and send that across for reconstruction or is it better to convert the whole class to_json and send it?</li>
  </ol>
  
  <p>If I create the class as is in the rails console and send it, I get a nice 200 and a response in the terminal.</p>
  
  <h3>More Details</h3>
  
  <p>(NOTE: only read this bit if you really want to know whats going on in the scenes)</p>
  
  <pre><code>@test.post_me
  {"myInteger":300,"myString":"testestest"}
  =&gt; {"x-ua-compatible"=&gt;["IE=Edge"], "etag"=&gt;["\"d41d8cd98f00b204e9800998ecf8427e\""], "connection"=&gt;["Keep-Alive"], "content-type"=&gt;["application/json; charset=utf-8"], "date"=&gt;["Thu, 14 Jul 2011 15:54:24 GMT"], "server"=&gt;["WEBrick/1.3.1 (Ruby/1.8.7/2009-06-12)"], "x-runtime"=&gt;["0.022517"], "content-length"=&gt;["0"], "set-cookie"=&gt;["_webApp_session=BAh7BiIPc2Vzc2lvbl9pZCIlZTJmM2IwYmVhZWMwM2E2ZGQzNWEwMDUwNmE2NDhlM2U%3D--5312eec4795d9f0633520c01992422e9c15746e4; path=/; HttpOnly"], "cache-control"=&gt;["max-age=0, private, must-revalidate"]}
  &gt;&gt; 
  </code></pre>
  
  <p>Heres the same response If i dont convert it to Json:</p>
  
  <pre><code>{"myInteger":300,"myString":"testestest"}
  =&gt; #&lt;Net::HTTPOK 200 OK  readbody=true&gt;
  </code></pre>
  
  <p>Please if someone could answer questions 1 and 2 I'd be greatful</p>
  
  <hr>
  
  <h3>Server Side</h3>
  
  <p>Heres the server side that needs to take the request and turn it back into Json and an object. Ive tried a bunch of different things which most have been commented out.</p>
  
  <p>Basically I'll either get a 200 response with the details in the tag but when I try to puts the hash object, I get nothing/</p>
  
  <pre><code>Started POST "/user_requests/add" for 127.0.0.1 at Thu Jul 14 16:56:07 +0100 2011
    Processing by UserRequestsController#add_request as 
    Parameters: {"{\"myInteger\":300,\"myString\":\"testestest\"}"=&gt;""}
  Rendered user_requests/add_request.json.erb (0.3ms)
  Completed 200 OK in 31ms (Views: 27.4ms | ActiveRecord: 0.0ms)
  </code></pre>
  
  <p>Heres the multiple attempt of breaking it apart and turning it back into an object.</p>
  
  <pre><code>  def add_request
      require 'rubygems'
      require 'json'
  
      #@custom = CustomRequest.new(params[:custom]) 
      #@custom = CustomRequest.new(JSON.parse(params[:custom]))
      #@custom = CustomRequest.new(params[:custom]).from_json
      #@custom = CustomRequest.new(params[:custom].from_json) 
  
  
       @myHash = Hash.new(params[:myHash])
       #@myHash = Hash.new(JSON.parse(params[:myHash]))
      #@myHash = Hash.new(params[:myHash]).from_json
      #@myHash = Hash.new(params[:myHash].from_json) 
  
  
       puts "IT WORKS!"
       puts @myHash.to_s
  </code></pre>
  
  <p>They're all pretty much the same except one was me trying to recreate the object from the sent hash and another was me trying to recreate hash_to_json to hash_from_json</p>
  
  <h3>More Questions</h3>
  
  <p>So:</p>
  
  <ol>
  <li>Am I creating the object correctly with the params(:myHash) or should i be referencing each variable individually?</li>
  <li>are the params automatically turned back into readable format or do i need to put to from or parse JSON. In regards to that, does it matter which one I use?</li>
  <li>Am i reconstructing the object correctly, why is putting my has displaying nothing?</li>
  <li>(optional) are there any easy ways to store it in a table thereafter?</li>
  </ol>