これは、Rails アプリケーションで Backbone を介してデータベース エントリを作成することに関する長い質問です。Rails コントローラーでデータを処理する方法と、バックボーンで URL を設定する方法について質問があります。
Rails にタイトル、テキスト、およびキーワード列を含む Docs テーブルを作成し、Rails に id 列を自動的に作成させました。コンソールで d7 と d8 という 2 つのドキュメントを作成しようとしました。
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"}
また、「DocsController#update undefined method 'stringify_keys' に NoMethodError を追加します」
タイトルのみで ID なしのドキュメント (d8) を作成する (Rails が手動で ID を設定)
d8 = new Doc({title: 'Document 8'})
Doc Constructor french2.js:24
child
ID を手動で設定せずにドキュメントを保存しようとすると、404 エラーが発生する
Object
POST http://localhost:3000/docs/undefined 404 (Not Found) jquery.js:8215
XHR finished loading: "http://localhost:3000/docs/undefined". jquery.js:8215
Object {readyState: 4, responseText: "<!DOCTYPE html>↵<html lang="en">↵<head>↵ <meta ch…ation on available routes.↵</p>↵↵</body>↵</html>↵", status: 404, statusText: "Not Found"}
4 つの質問:
1) Rails リソースを操作するために、Backbone Doc モデルで URL が正しく設定されていますか。 this.url = "docs/" + this.id
完全なコードについては、以下のドキュメント モデルを参照してください。
d7.save()
2)ドキュメントを作成するためにsave を呼び出す必要が ありますか? Rails docs コントローラーで更新アクションがトリガーされていることに気付きましたか?
3) stringify キー エラーを発生させずに文字列を保存するにはどうすればよいですか?
4) Rails コントローラー: 私の docs テーブルには 3 つのフィールド (タイトル、キーワード、テキスト) と、レールが追加するものがあります。各フィールドを params の列名 (つまり、params[:title][:keywords]) で明示的に識別する必要がありますか?それとも、シンボル :doc のみが指定されている以下のコード (他の人からコピーしたもの) は正しいですか? ? パラメータの名前は任意ですか?
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.Doc = Backbone.Model.extend({
initialize : function Doc() {
this.url = "docs/" + this.id #not sure if this is correct
this.bind("error", function(model, error){
console.log( error );
});
},
URL が「docs」に設定されたコレクション
window.Docs = Backbone.Collection.extend({
model : Doc,
url: "docs",
initialize : function() {
console.log('Docs collection Constructor');
}
});
Rails doc コントローラー
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
いくつかのシード データを作成しましたが、タイトルのみを設定しました。Rails は id を自動的に設定しますが、手動で行うことはできますか?
Doc.create!(title: "doc 1")
Doc.create!(title: "doc 2")