Question

So i found this strange anomaly while working in a gem that we are using internally.

We have this private method

private
def redirect_to_element(element, next_upload)
  send("scorecard_#{element.base_class_name.underscore}_path", current_scorecard, current_tab(element, next_upload))
end

Which just builds a path dynamically depending on what element is passed to it. What i would like to do is have those dynamic paths pass some params. But i get this error

undefined method `scorecard_enterprise_development_path(ignore_tracking: true)' for #<#<Class:0x007ff767a702e0>:0x007ff767899a20>

so in the console i tried several things and this is what i found.

>> scorecard_enterprise_development_path
=> "/scorecards/338/enterprise_development"

>> send('scorecard_enterprise_development_path')
=> "/scorecards/338/enterprise_development"

>> scorecard_enterprise_development_path(ignore_tracking: true)
=> "/scorecards/338/enterprise_development?ignore_tracking=true"

>> send('scorecard_enterprise_development_path(ignore_tracking: true)')
!! #<NoMethodError: undefined method `scorecard_enterprise_development_path(ignore_tracking: true)' for #<#<Class:0x007ff767a702e0>:0x007ff767899a20>>

That using the send method to build a path with params will fail. Can anyone explain why this happens?

I am using, ruby -v 1.9.3p327 and rails -v 3.2.16

Was it helpful?

Solution

#send will invoke the method identified by the first argument and pass it any arguments specified.

So you should use the method this way:

send('scorecard_enterprise_development_path', ignore_tracking: true)

See the send documentation

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top