I'm trying to use passing options in representers 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)
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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.