[英]Issue passing options for a representable - wrong number of arguments (1 for 0)
我正在嘗試在表征gem中使用傳遞選項
我有一個裝飾器:
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
但是當我嘗試使用以下方法傳遞選項時:
ProductDecorator.new(product).
to_hash(store_id: 1, site: "my_url")
我收到wrong number of arguments (1 for 0)
消息wrong number of arguments (1 for 0)
我嘗試了很多選擇,但是我可以找到正在發生的事情。
to_hash
方法不接受錯誤的參數。
我想你打算做這樣的事情:
ProductDecorator.new(product).to_json(store_id: 1, site: "my_url")
或者,您也可以申請:
JSON.parse(ProductDecorator.new(product).
to_json(store_id: 1, site: "my_url")).symbolize_keys
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.