2014-03-30 3 views
0

안녕하세요, 레일즈 4를 사용하여 아래 오류가 표시됩니다. 프론트 엔드 (EmberJs)가 json을 제공합니다.Rails4 NoMethodError (정의되지 않은 메소드`[] 'for nil : NilClass) :

는 ArticlesController 번호로 2014년 3월 30일 0시 54분 18초 -0400 처리에 127.0.0.1에 대한 POST "/ 기사"를 시작 만들 JSON 매개 변수로 : { "기사"=> { "제목"=> "제목", "본체"= ""콘텐츠 ","author_id "=", "2", "저자"=> 닐}} 허가되지 않은 파라미터 : 4782ms

NoMethodError 500 완료 저작 (미정 방법 []' for nil:NilClass): app/controllers/articles_controller.rb:43:in 블록 생성 ' app/controllers/articles_controller.rb : 42 : 생성 중

내 모델은 다음과 같습니다 :

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 
+0

기사 컨트롤러에서 43 행은 무엇입니까? 네가 올린 책에는 43 줄조차 없다. 또한 모델에서 attr_accessible을 사용하는 이유는 무엇입니까?하지만 컨트롤러의 강력한 매개 변수는 무엇입니까? –

+0

attr_accesible로 문제를 해결하려고했지만 작동하지 않습니다. 43 번째 줄 (컨트롤러의 추출물)은 강력한 매개 변수 @ ArticleImage = Article.new (article_params)의 호출에 해당합니다. 왜 이해가 안되는 이유는 배열에 액세스하려고 시도하고 정의되지 않은 메서드 [] 오류가 발생하는 경우입니다. – utnas

답변

1

gemfile에서 보석 'protected_attributes'을 제거하여 해결.

관련 문제