Passing argument to decorator from controller in Rails using Draper
-
04-07-2021 - |
Question
I couldn't figure out how to pass an argument to a decorator from a controller:
The decorator:
def as_json(options = nil)
{
:name => user.name,
:dob => user.dob
:created_at => user.created_at,
:url => user
}
end
The controller:
format.json { render :json => UserJsonDecorator.new(@user)}
Just passing an extra argument to the new method does not work:
UserJsonDecorator.new(@user,options)
Any ideas?
Solution
I was basically using it wrong.
correct form to pass additional arguments is:
UserJsonDecorator.new(@user).to_json(options)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow