[英]Rails create action undefined method 'each' for nil:NilClass
在我的Rails應用程序中,我正在創建一個帶有contentEditable div的表單,因此我必須通過AJAX“ PUT”請求將所有數據手動發送到控制器。 但是,create操作不再起作用,並引發以下錯誤:
Started POST "/submissions" for 98.245.21.165 at 2013-09-25 20:57:51 +0000
Processing by SubmissionsController#create as JSON
Parameters: {"title"=>"fyc", "content"=>"fuc", "folder_id"=>"1"}
User Load (80.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
(81.3ms) SELECT COUNT(*) FROM "submissions" WHERE "submissions"."title" = 'fyc'
Completed 500 Internal Server Error in 181ms
NoMethodError (undefined method `each' for nil:NilClass):
app/controllers/submissions_controller.rb:16:in `create'
這是我在submissions控制器中的create動作:
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
第16行(Rails拋出NoMethodError處)在if @submissions.empty?
在其中創建Submission實例的位置。
我唯一的想法是,也許是在將參數放入ajax_變量的頂部,它返回nil? 我傾斜了路線,可以看到我確實有一個/submissions
PUT請求,該請求路由到此create動作,所以這不是問題。
這是有問題的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");
}
});
});
就像我說的那樣,我使用HTML5 contentEditable div作為表單輸入,所以據我所知,我不能真正依靠Rails為我進行AJAX調用。 該錯誤僅在最近才開始發生,我不確定它是如何開始的。
有任何想法嗎? 在這一點上我很絕望。
更新這里是我的Submission.rb模型:
class Submission < ActiveRecord::Base
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')
attr_accessible :content, :title, :user_id, :folder_id, :parent_id, :children
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
和我的Folder.rb模型:
class Folder < ActiveRecord::Base
attr_accessible :title, :user_id, :parent_id
belongs_to :user
belongs_to :parent, :class_name => 'Folder'
has_many :children, :class_name => 'Folder', :foreign_key => 'parent_id'
has_many :submissions, :order => ('updated_at DESC')
def self.search(search)
where("title like ?", "%#{search}%")
end
end
因為submission has_many children
Submission.create(:children => nil)
將拋出以下錯誤,因為它期望為children
分配一個數組
NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
但是Submission.create(:children => [])
不會引發錯誤。
在您的示例中, params[:children]
為nil
,因此引發了錯誤。 因此,在控制器中您可以執行
ajax_children = Array(params[:children])
#=> [] if params[:children] is nil
這會將nil
轉換為[]
,如果參數為[1,2,3]
則保持不變。
通常,請確保傳遞給模型方法的值在控制器中采用正確的格式,因為用戶輸入可能會有很大差異。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.