I set up Draper's Decorator for a Rails project to replace the original helper. I moved the helper code to the Decorator:
def birthday(date)
"MinYear#{date.year - 1911} Month#{date.month} Day#{date.day}"
end
And add .decorate text in View to use it
<%= user.decorate.birthday %>
However, this will cause an ArgumentError wrong number of arguments (given 0, expected 1)
def birthday(date)
If I remove the first date, it will appear a NoMethodError undefined method 'date' for #<UserDecorator:0x000055570cbafc58>
"MinYear#{date.year - 1911} Month#{date.month} Day#{date.day}"
How can I fix these errors?
That
ArgumentErroris raised because you callbirthdaywithout passing in adate.Doing
<%= user.decorate.birthday(Date.current) %>will make the error go away.But, this is probably not what you want since
dateis defined in theUsermodel.The easiest way to decorate something is to use delegate_all. It will make all methods from the
Usermodel available in the decorator.Note that any methods declared in the decorator will override those declared in the model, in your case
birthday. For example, if you have other methods inside the decorator that callbirthday, it will use the one from the decorator, not that from the model, which will be available by callingmodel.birthday.Another approach is to call methods on the model object :