Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Store json object in a ruby on rails application

I have an Json object that I have to store in a ruby on rails application. How can I store a json object in an ruby on rails application? What is need to pass a json object to the controller and invoke que create method?

I have tried with this jquery ajax request like this:

function ajaxsend() { 
  $.ajax({
    url: "http://localhost:3000/articles",
    type: "POST",
    data: { "article": { "title": "firsttitle", "text": "text of the article" } }              });

}

But I don't get nothing. I tried to see if I get something with this controller:

    def create
        render plain: params[:article].inspect
    end

but nothing happens.

Can someone point me to a solution? How can I store a json object into an ruby on rails app?

1 Answer

The first thing I would do is specify the dataType in your ajax request. The default in Rails is html. So when you send that ajax request to the create action it will try and render a view in html.erb. I would either set dataType: 'json' in your ajax request or change the route of the request to url: "http://localhost:3000/articles.json".