简体   繁体   English

发出可表示的传递选项-参数数目错误(1代表0)

[英]Issue passing options for a representable - wrong number of arguments (1 for 0)

I'm trying to use passing options in representers gem 我正在尝试在表征gem中使用传递选项

I have a decorator: 我有一个装饰器:

class ProductDecorator < Representable::Decorator
  include Representable::JSON

  property :code
  property :name

  property :store_id, getter: lambda { |opts| opts[:store_id] },
    type: Integer

  property :url, decorator_scope: true, getter: lambda { |options|
    File.join(options.fetch(:site,""), url)
  }, type: String

  def url
    File.join(represented.path ,"p")
  end

end

But when I try to pass my options using: 但是当我尝试使用以下方法传递选项时:

ProductDecorator.new(product).
        to_hash(store_id: 1, site: "my_url")

I'm getting the message wrong number of arguments (1 for 0) 我收到wrong number of arguments (1 for 0)消息wrong number of arguments (1 for 0)

I've tried many options, but I could find what's going on. 我尝试了很多选择,但是我可以找到正在发生的事情。

to_hash method doesn't take arguments that is the reason for error. to_hash方法不接受错误的参数。

I suppose you meant to do something like: 我想你打算做这样的事情:

ProductDecorator.new(product).to_json(store_id: 1, site: "my_url")

Or you can also go for: 或者,您也可以申请:

JSON.parse(ProductDecorator.new(product).
to_json(store_id: 1, site: "my_url")).symbolize_keys

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM