Sign in through GitHub

Please read for an updated status on RailsCasts:

Learn more or hide this

Travis Womble's Profile

GitHub User: twamble

Comments by Travis Womble

Avatar

When I was going through the RailsCast as described above. I was using the entries controller that Ryan put together. After much searching, copying, pasting, and retrying I found that I need a completely new Controller set up. Below is what I originally had.

class EntriesController < ApplicationController
  respond_to :json

  def index
    respond_with Entry.all
  end

  def show
    respond_with Entry.find(params[:id])
  end

  def create
    respond_with Entry.create(params[:entry])
  end

  def update
    respond_with Entry.update(params[:id], params[:entry])
  end

  def destroy
    respond_with Entry.destroy(params[:id])
  end
end

This is the Controller code the fixed the issue for me.

class EntriesController < ApplicationController
def index
    render :json => Entry.all
  end

def show
  render :json => Entry.find(params[:id])
end

def create
  entry = Entry.create! params
  render :json => entry
end

def update
  entry = Entry.find(params[:id])
  entry.update_attributes!(params[:entry])
  render :json => entry
end

def destroy
  render :json => Entry.destroy(params[:id])
end
end

Thanks All!

Travis

Avatar

Can someone help me understand how to make this screencast work with monodb as my database while using mongoid. This is what I am seeing.

When I create a new entry via the console, similar to how Ryan did it in the video 'entry.create' , rails adds that entry just fine. Below are is my Ruby Log and my javascript headers log from Chrome Inspector.

Ruby Log

Started POST "/api/entries" for 127.0.0.1 at 2012-02-12 17:31:24 -0600
Processing by EntriesController#create as JSON
Parameters: {"name"=>"Heather", "entry"=>{"name"=>"Heather", "action"=>"create", "controller"=>"entries"}}
MONGODB w_market_development['system.namespaces'].find({})
MONGODB w_market_development['entries'].insert([{"_id"=>BSON::ObjectId('4f384bcc504b9348be000003'), "name"=>"Heather"}])
Completed 201 Created in 11ms (Views: 2.4ms)

Headers Log

Request URL:http://0.0.0.0:3000/api/entries
Request Method:POST
Status Code:201 Created
Request Headers (14)
Request Payload
{"name":"Heather"}

As you can see it posted fine. Now let me show you an update via the 'entry.save()' example Ryan showed us.

Ruby Log

Started POST "/api/entries" for 127.0.0.1 at 2012-02-12 17:34:25 -0600
Processing by EntriesController#create as JSON
Parameters: {"_id"=>"4f38152c504b9345dc000005", "name"=>"Bloip", "winner"=>true, "entry"=>{"_id"=>"4f38152c504b9345dc000005", "name"=>"Bloip", "winner"=>true, "action"=>"create", "controller"=>"entries"}}
MONGODB w_market_development['system.namespaces'].find({})
MONGODB w_market_development['entries'].insert([{"_id"=>BSON::ObjectId('4f38152c504b9345dc000005'), "name"=>"Bloip", "winner"=>true}])
Completed 201 Created in 12ms (Views: 2.7ms)

Headers Log

Request URL:http://0.0.0.0:3000/api/entries
Request Method:POST
Status Code:201 Created
Request Headers (14)
Request Payload
{"_id":"4f38152c504b9345dc000005","name":"Bloip","winner":true}


As you can see when I complete the 'entry.save()' on a current entry, which should be an update, the json is showing a POST instead of a PUT which mongoid is doing nothing with and the DB shows no changes. After googleing I found the following articles but nothing really helped.

link
link

I hope someone can help.