Подтвердить что ты не робот

Ошибка: при назначении атрибутов вы должны передать хэш в качестве аргумента

Привет, только что началось с ruby, и я писал контроллер и спецификацию контроллера, но у меня есть некоторые проблемы.

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

однако, когда я запустил bundle exec rspec spec, чтобы опробовать мои тесты, я получил сообщение об ошибке:

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)>'

Может ли кто-нибудь пролить свет на это?

4b9b3361

Ответ 1

Проблема в вашем контроллере.

  def create
    @document = Document.new(:name) # :name is a symbol, not a Hash like params for example