Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 主干与Rails资源同步_Ruby On Rails_Backbone.js - Fatal编程技术网

Ruby on rails 主干与Rails资源同步

Ruby on rails 主干与Rails资源同步,ruby-on-rails,backbone.js,Ruby On Rails,Backbone.js,关于通过主干在Rails应用程序中创建数据库条目,这是一个很长的问题。我对如何在Rails控制器中处理数据以及如何在主干中设置URL有疑问 我在rails中创建了一个包含标题、文本和关键字列的Docs表,让rails自动创建id列。我试图在控制台中创建两个文档,d7和d8 在d7中,我尝试手动设置id和标题。当我试图“保存”它时,它给了我500个错误 在d8中,我制作了一个只有标题的文档。当我试图保存它时,它给了我一个404错误 使用手动设置的id和标题创建文档(d7) d7 = new Doc

关于通过主干在Rails应用程序中创建数据库条目,这是一个很长的问题。我对如何在Rails控制器中处理数据以及如何在主干中设置URL有疑问

我在rails中创建了一个包含标题、文本和关键字列的Docs表,让rails自动创建id列。我试图在控制台中创建两个文档,d7和d8

在d7中,我尝试手动设置id和标题。当我试图“保存”它时,它给了我500个错误

在d8中,我制作了一个只有标题的文档。当我试图保存它时,它给了我一个404错误

使用手动设置的id和标题创建文档(d7)

d7 = new Doc({ id: '007', title: 'Document 7'})
Doc Constructor french2.js:24
child
试图保存d7会产生500错误

d7.save({}, { success : function(rec) {console.log('saved : ', rec); } })

    PUT http://localhost:3000/docs/007 500 (Internal Server Error) jquery.js:8215
    XHR finished loading: "http://localhost:3000/docs/007". jquery.js:8215
    Object {readyState: 4, responseText: "<!DOCTYPE html>↵<html lang="en">↵<head>↵  <meta ch…ders</b>: <pre>None</pre></p>↵↵↵↵</body>↵</html>↵", status: 500, statusText: "Internal Server Error"}
尝试在没有手动设置id的情况下保存文档会产生404错误

     def create
        respond_with Doc.create(params[:doc])
     end 
     def update
       respond_with Doc.create(params[:id], params[:doc])
     end 
代码

url设置为
this.url=“docs/”+this.id

window.Docs = Backbone.Collection.extend({
            model : Doc,

            url: "docs",


            initialize : function() {
                console.log('Docs collection Constructor');
            }
        });
url设置为“文档”的集合

Rails文档控制器

class CreateDocs < ActiveRecord::Migration
  def change
    create_table :docs do |t|
      t.string :title
      t.string :text
      t.string :keywords

      t.timestamps
    end
  end
end

至于模型上的url属性,为了直接在模型上保存(而不是通过Collection.create),您必须检查它是新条目还是更新。这段代码对url属性执行此操作,并允许您为创建和更新调用obj.save()

def create
    respond_with Document.create(params[:doc])
end 
对于Rails控制器来说,这样做就足够了

window.Docs = Backbone.Collection.extend({
            model : Doc,

            url: "docs",


            initialize : function() {
                console.log('Docs collection Constructor');
            }
        });
class DocsController < ApplicationController

  respond_to :json

    def index
        respond_with Doc.all
    end 

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

    end 

    def create
        respond_with Doc.create(params[:doc])
    end 

    def update
        respond_with Doc.create(params[:id], params[:doc])
    end 

    def destroy
        respond_with Doc
    end    

end
class CreateDocs < ActiveRecord::Migration
  def change
    create_table :docs do |t|
      t.string :title
      t.string :text
      t.string :keywords

      t.timestamps
    end
  end
end
Doc.create!(title: "doc 1")
Doc.create!(title: "doc 2")
 url : function() {
  var base = 'documents';
  if (this.isNew()) return base;
  return base + (base.charAt(base.length - 1) == '/' ? '' : '/') + this.id;
}
def create
    respond_with Document.create(params[:doc])
end