0
你好,我使用Rails 4得到了下面的錯誤。我的前端(EmberJs)提供了json。Rails4 NoMethodError(未定義的方法'[]'爲零:NilClass):
開始POST「/ articles」爲127.0.0.1於2014-03-30 00:54:18 -0400 通過ArticlesController處理#create as JSON 參數:{「article」=> {「title」=> 「標題」, 「身體」=> 「內容」, 「AUTHOR_ID」=> 「2」, 「作者」=>無}} 未經許可參數:在4782ms
NoMethodError完成500作者 (未定義的方法[]' for nil:NilClass): app/controllers/articles_controller.rb:43:in
塊創建' app/controllers/articles_controller.rb:42:'create'
我的模型看起來像這樣:
class Article < ActiveRecord::Base
validates :title, presence: true
validates :body, presence: true
attr_accessible :title, :body, :author_id
belongs_to :author
end
我的控制器看起來是這樣的:
class ArticlesController < ApplicationController
def new
@article = Article.new
respond_to do |format|
format.json { render json: @article }
end
end
def create
@article = Article.new(article_params)
respond_to do |format|
if @article.save
format.json { render json: @article, status: :created, location: @article }
else
format.json { render json: @article, status: :unprocessable_entity }
end
end
end
private
def article_params
params.require(:article).permit(:title, :body, :author_id)
end
end
什麼是您的物品控制器中的第43行?你發佈的那個甚至沒有43行。另外,爲什麼在模型中使用attr_accessible,但在控制器中使用強參數? –
我試圖用attr_accesible來解決這個問題,但它不起作用。第43行(它是控制器的摘錄)對應於強參數@article = Article.new(article_params)的調用。我不明白的是爲什麼當它試圖訪問數組並獲取未定義的方法[]錯誤。 – utnas