0

在通过 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)

因此,您可以看到我的表单和路线有效,因为启动了正确的操作并且传递了参数。但是,参数不会提交到模型的新实例中。

这是我的创建操作:

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");
                }
            });
        });

我什至检查了我的 submit.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

4

1 回答 1

0

尝试更改您的 AJAX 请求格式。如果你看看你的路线:

submissions GET    /submissions(.:format)  submissions#index
            POST   /submissions(.:format) submissions#create

它期待一个.format. 所以对于 JSON 它应该是/submissions.json. 该框架旨在为您解决这个问题,但由于您是手动完成的,因此您可能需要更加具体。

于 2013-09-25T18:59:18.670 回答