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

发布于 2025-01-05 02:10:45 字数 2427 浏览 0 评论 0原文

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

这就是我所看到的。

当我通过控制台创建一个新条目时,类似于 Ryan 在视频中使用 entry.create 所做的那样,Rails 会很好地添加该条目。下面是来自 Chrome Inspector 的 Ruby 日志和 JavaScript 标头日志。

Ruby 日志

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 日志

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/

Can someone help me understand how to make Ryan Bate's screencast on Backbone.js work with MongoDB as my database while using the Mongoid gem.

This is what I am seeing.

When I create a new entry via the console, similar to how Ryan did it in the video with entry.create, Rails adds that entry just fine. Below 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 Googling I found the following articles but nothing really helped.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘿哥们儿 2025-01-12 02:10:45

当我如上所述浏览 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

谢谢大家!

特拉维斯

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文