0

Hello I'm getting the error below by using Rails 4. My front-end(EmberJs) provides json.

Started POST "/articles" for 127.0.0.1 at 2014-03-30 00:54:18 -0400 Processing by ArticlesController#create as JSON Parameters: {"article"=>{"title"=>"Title", "body"=>"Content", "author_id"=>"2", "author"=>nil}} Unpermitted parameters: author Completed 500 in 4782ms

NoMethodError (undefined method []' for nil:NilClass): app/controllers/articles_controller.rb:43:inblock in create' app/controllers/articles_controller.rb:42:in `create'

My Model looks like this:

class Article < ActiveRecord::Base
 validates :title, presence: true
 validates :body, presence: true

 attr_accessible :title, :body, :author_id

 belongs_to :author
end

My controller looks like this:

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
2
  • What is line 43 in your articles controller? The one you posted doesn't even have 43 lines. Also, why are you using attr_accessible in your model, but strong parameters in your controller? Commented Mar 30, 2014 at 14:58
  • I tried to solve the problem with attr_accesible but it does'nt works. the line 43(it's an extract of the controller) corresponds to the call of strong parameters @article = Article.new(article_params). What I don't understand is why and when it tries to access to the array and gets undefined method []' error. Commented Mar 31, 2014 at 1:19

1 Answer 1

1

Solved by removing gem 'protected_attributes' from gemfile.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.