简体   繁体   English

Rails / Ruby send方法无法用params构建路径?

[英]Rails/Ruby send method can't build paths with params?

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. 使用send方法构建带有params的路径将失败。 Can anyone explain why this happens? 谁能解释为什么会这样?

I am using, ruby -v 1.9.3p327 and rails -v 3.2.16 我正在使用,ruby -v 1.9.3p327和rails -v 3.2.16

#send will invoke the method identified by the first argument and pass it any arguments specified. #send将调用第一个参数标识的方法,并将指定的任何参数传递给它。

So you should use the method this way: 所以你应该这样使用这个方法:

send('scorecard_enterprise_development_path', ignore_tracking: true)

See the send documentation 请参阅发送文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM