使用通过AJAX PUT请求提交表单数据的Rails应用程序,不知何故我打破了一切,因为它曾经工作正常。首先让我向您展示我在控制台中收到的错误消息:
Started POST "/submissions" for 98.245.21.165 at 2013-09-25 18:10:49 +0000
Processing by SubmissionsController#create as JSON
Parameters: {"title"=>"I'll be up in marikesh", "content"=>"Yup", "folder_id"=>"1"}
User Load (86.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
(86.3ms) BEGIN
SQL (178.9ms) INSERT INTO "submissions" ("content", "created_at", "folder_id", "parent_id", "title", "updated_at", "user_id") VALUES ($1, $2, $3, $4, $5, $6
, $7) RETURNING "id" [["content", nil], ["created_at", Wed, 25 Sep 2013 18:10:50 UTC +00:00], ["folder_id", nil], ["parent_id", nil], ["title", nil], ["update
d_at", Wed, 25 Sep 2013 18:10:50 UTC +00:00], ["user_id", nil]]
(90.9ms) COMMIT
(85.9ms) BEGIN
(86.3ms) COMMIT
Rendered submissions/create.html.erb within layouts/application (0.5ms)
Completed 200 OK in 701ms (Views: 52.6ms | ActiveRecord: 614.3ms)
因为你可以看到我的表格和路线有效,因为启动了正确的动作并且传递了参数。但是,params没有被提交到模型的新实例中。
这是我的创作动作:
def create
ajax_title = params[:title]
ajax_content = params[:content]
ajax_folder = params[:folder_id]
ajax_parent = params[:parent_id]
ajax_children = params[:children]
@submissions = Submission.where(title: ajax_title)
if @submissions.empty?
@submission = Submission.create({title: ajax_title, content: ajax_content, user_id: current_user.id, folder_id: ajax_folder, parent_id: ajax_parent, children: ajax_children})
else
@submissions[0].content = ajax_content
@submissions[0].save
end
end
这是我的AJAX表格:
$("#save-entry").click(function(){
$.ajax({
type: "POST",
url: "/submissions",
dataType: "json",
data: {title: $("#title-create-partial").text(), content: $("#content-create-partial").text(), folder_id: <%= @folder.id %>},
complete: function(){
$.get("/ajax_load_events/", {folder: <%= @folder.id %>}, null, "script");
}
});
});
我甚至检查了我的submission.rb模型,并且所有属性都是attr_accessible,所以这不应该是问题。这里发生了什么想法?关于如何改变/改进我的创建行动的建议也将受到赞赏。我实际上不需要检查唯一性(用户应该能够创建具有相同标题的多个提交),因此如果您对如何简化操作有任何建议,那也将不胜感激。
这是submission.rb:
class Submission < ActiveRecord::Base
attr_accessible :content, :title, :user_id, :folder_id, :parent_id, :children
belongs_to :user
belongs_to :folder
belongs_to :parent, :class_name => 'Submission'
has_many :children, :class_name => 'Submission', :foreign_key => 'parent_id', :order => ('updated_at DESC')
def self.search(search)
if search
where('name LIKE ?', "%#{search}%")
else
scoped
end
end
def attributes_with_children
attributes.merge(children: children.map(&:attributes_with_children))
end
end
添加logger.info(@submission.inspect)
并获得此输出:
Started POST "/submissions" for 98.245.21.165 at 2013-09-25 18:56:23 +0000
Processing by SubmissionsController#create as JSON
Parameters: {"title"=>"test", "content"=>"test", "folder_id"=>"1"}
User Load (77.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
(83.5ms) SELECT COUNT(*) FROM "submissions" WHERE "submissions"."title" = 'test'
Completed 500 Internal Server Error in 181ms
NoMethodError (undefined method `each' for nil:NilClass):
app/controllers/submissions_controller.rb:16:in `create'
我倾斜了我的路线,这里是:http://pastebin.com/gdfthakg
答案 0 :(得分:0)
尝试更改您的AJAX请求格式。如果你看一下你的路线:
submissions GET /submissions(.:format) submissions#index
POST /submissions(.:format) submissions#create
期待.format
。所以对于JSON,它应该是/submissions.json
。该框架旨在为您解决此问题,但由于您手动执行此操作,因此您可能需要更具体。