Ruby-on-rails 错误:分配属性时,您必须将散列作为参数传递

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25115017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:51:04  来源:igfitidea点击:

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

ruby-on-railsruby

提问by Hanzawa Naoki

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

嗨,我刚开始使用 ruby​​,我正在编写控制器和控制器规范,但我遇到了一些问题。

document.rb

文件.rb

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

documents_controller.rb

文件控制器.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

文档控制器规范.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 specto try out my tests i got an error:

然而,当我跑去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)>'

Can anyone shed some light on this?

任何人都可以对此有所了解吗?

采纳答案by Yanis Vieilly

The problem is in your controller.

问题出在您的控制器上。

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