wrong number of arguments (given 2, expected 1) in Rails

161 views Asked by At
module ApplicationHelper
  def t(*a)
    translate(*a)
  end
end

Case 1 :- t('views.home.welcome_updated', default: 'Updated') => Getting Error: (wrong number of arguments (given 2, expected 1))

Case 2 :- t('views.welcome') => Working fine

Ruby Version: 3.0.6
Rails Version: 6.1.7.6

What is the problem with above code?

Both case 1 & 2 working fine in ruby 2.7. i am getting error after upgraded ruby version to 3

customize t method to

module ApplicationHelper
  def t(key, **options)
    args = [key]
    args.push options if options.present?
    translate(*a)
  end
end

But this is also not working

1

There are 1 answers

0
spickermann On

When you want to forward all arguments, not matter of their structure, then you can use the ... syntax, like this:

module ApplicationHelper
  def t(...)
    translate(...)
  end
end

Or, when do not need to manipulate the arguments before forwarding to the original method or to act on the response before returning, then you can alias the method name like this:

alias_method :t, :translate