Backbone Mongoid Rails设置不会让我更新记录

时间:2012-02-13 01:24:45

标签: ruby-on-rails ruby backbone.js mongoid

在使用Mongoid gem时,有人可以帮助我了解如何让Backbone.js上的Ryan Bate's screencast使用MongoDB作为我的数据库。

这就是我所看到的。

当我通过控制台创建一个新条目时,类似于Ryan在entry.create的视频中所做的那样,Rails补充说该条目就好了。以下是我的Ruby日志和Chrome Inspector的JavaScript头日志。

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)

标题日志

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

正如你所看到它发布的好。现在让我通过Ryan向我们展示的entry.save()示例向您展示更新。

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)

标题日志

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}

正如您所看到的,当我在当前条目上完成entry.save()时,应该是更新,JSON显示的是POST而不是PUT,Mongoid没有做任何事情,数据库显示没有更改。谷歌搜索后,我发现了以下文章,但没有任何帮助。

https://github.com/codebrew/backbone-rails/issues/8 http://lostechies.com/derickbailey/2011/06/17/making-mongoid-play-nice-with-backbone-js/

1 个答案:

答案 0 :(得分:2)

当我按照上述方法浏览RailsCast时。我正在使用Ryan放在一起的条目控制器。经过大量搜索,复制,粘贴和重试后,我发现我需要一个全新的控制器设置。以下是我原来的情况。

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

这是修复了我的问题的控制器代码。

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

全部谢谢!

特拉维斯