Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to write JSON to Rails Database
    primarykey
    data
    text
    <p>I am new to rails and trying to understand how this works for several days now. My goal is to post a JSON-file from the browser (different domain) to an Rails application and save the sent data to the database.</p> <p>Following JQuery-Code is executed:</p> <pre><code>$(document).ready(function() { $("#testbutton").click(function() { var params = '{"comment": "Test", "creator": "Max", "name": "Testname", "url": "www.foo.com"}'; $.ajax({ type: "post", headers: { 'X-CSRF-Token': '&lt;%= form_authenticity_token.to_s %&gt;' }, accepts: "application/json", contents: "application/json", data: params, url: 'http://localhost:3000/pages' }); }); }); </code></pre> <p>I skip verifying the autheticity-token in the action-controller:</p> <pre><code>skip_before_filter :verify_authenticity_token </code></pre> <p>Now, I don't know what to do in the create-action what I have to change that it will parse the JSON and write it to the database. I use the standard-behavior of the create-action:</p> <pre><code> def create @page = Page.new(params[:data]) respond_to do |format| if @page.save format.html { redirect_to @page, :notice =&gt; 'Page was successfully created.' } format.json { render :json =&gt; @page, :status =&gt; :created, :location =&gt; @page } else format.html { render :action =&gt; "new" } format.json { render :json =&gt; @page.errors, :status =&gt; :unprocessable_entity } end end end </code></pre> <p>The server receives the data from the website, but isn't able to parse it.</p> <pre><code>Started POST "/pages" for 127.0.0.1 at Thu Jun 07 21:48:53 +0200 2012 Processing by PagesController#create as undefined Parameters: {"{\"data\": {\"comment\": \"Test\", \"creator\": \"Fam Disselhoff\", \"name\": \"Renate\", \"url\": \"www.elesenroth.de\"}}"=&gt;nil} (0.1ms) begin transaction SQL (67.5ms) INSERT INTO "pages" ("comment", "created_at", "creator", "name", "updated_at", "url") VALUES (?, ?, ?, ?, ?, ?) [["comment", nil], ["created_at", Thu, 07 Jun 2012 19:48:53 UTC +00:00], ["creator", nil], ["name", nil], ["updated_at", Thu, 07 Jun 2012 19:48:53 UTC +00:00], ["url", nil]] </code></pre>
    singulars
    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.
    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