Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: When assigning attributes, you must pass a hash as an argument

Hi ive just started with ruby and i was writing the controller and controller spec but i have some problems.

document.rb

class Document < ActiveRecord::Base
  validates_presence_of :name
  has_many :votes
end

documents_controller.rb

class API::DocumentsController < ApplicationController
  def create
    @document = Document.new(:name)
    if @document.save
      @document.update_attributes(name: @document.name.url) if document_params[:name].present?
      render json: @document, serializer: DocumentSerializer, status: 201
    else
      render json: @document.errors, status: 422
    end
  end
end

documents_controller_spec.rb

  describe "POST 'index'" do
    before { @attr = FactoryGirl.attributes_for(:document) }

    describe "failure" do
      describe "with missing parameters" do
        before { @attr.each { |key,value| @attr[key] = nil } }
        it "should not create a new record" do
          lambda { post :create, document: @attr }.should_not change(Document, :count)
        end
      end
    end
  end

however, when i ran bundle exec rspec spec to try out my tests i got an error:

Failure/Error: lambda { post :create, document: @attr }.should_not change(Document, :count)                                                                                                                                                                                  
     ArgumentError:                                                                                                                                                                                                                                                               
       When assigning attributes, you must pass a hash as an argument.                                                                                                                                                                                                            
     # ./app/controllers/api/documents_controller.rb:8:in `create'                                                                                                                                                                                                                
     # ./spec/controllers/api/documents_controller_spec.rb:29:in `block (6 levels) in <top (required)>'                                                                                                                                                                           
     # ./spec/controllers/api/documents_controller_spec.rb:29:in `block (5 levels) in <top (required)>'

Can anyone shed some light on this?

like image 967
Hanzawa Naoki Avatar asked Aug 04 '14 08:08

Hanzawa Naoki


1 Answers

The problem is in your controller.

  def create
    @document = Document.new(:name) # :name is a symbol, not a Hash like params for example
like image 165
Yanis Vieilly Avatar answered Nov 15 '22 03:11

Yanis Vieilly