Note that there are some explanatory texts on larger screens.

plurals
  1. PORails controller does not accept JSON?
    primarykey
    data
    text
    <p>I am trying to create an ActiveRecord object via a JSON request. However the controller fails to set the variables passed in the parameters in the newly created object. As an example, a person object has two fields: firstname and lastname.</p> <p>The JSON generated by the JSON.stringify function of the JSON.org library produces:</p> <pre><code>{"firstname" : "Joe" , "lastname" : "Bloggs"} </code></pre> <p>However the controller expects the JSON to be in the form:</p> <pre><code>{ "Person" : {"firstname" : "Joe" , "lastname" : "Bloggs"} } </code></pre> <p>I am aware that in the normal course of events (for HTTP requests) the parameters for the request are nested under the class name of the model being created. </p> <p>The create action in the controller is:</p> <pre><code>def create @person = Person.new(params[:person]) respond_to do |format| if @person.save flash[:notice] = 'Person was successfully created.' format.html { redirect_to(@person) } format.xml { render :xml =&gt; @person, :status =&gt; :created, :location =&gt; @person } format.json { render :json =&gt; @person, :status =&gt; :created, :location =&gt; @person } else format.html { render :action =&gt; "new" } format.xml { render :xml =&gt; @person.errors, :status =&gt; :unprocessable_entity } format.json { render :json =&gt; @person.errors, :status =&gt; :unprocessable_entity } end end end </code></pre> <p>What would be the neatest way for the controller to process the JSON requests as generated? Or alternatively how do you generate "correct" JSON from Javascript objects for passing to your controllers?</p> <p>TIA, Adam</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload