如何调用表示为字符串的方法



我有这个代码:

def show_block_path
  Rails.application.routes.url_helpers.gate_path(resource_id))
end

我正在尝试必须重构它:

def show_block_path
  Rails.application.routes.url_helpers."{resource_type.downcase}"_path(resource_id))
end

resource_type.downcase "gate",但该方法不起作用。为什么?

使用 Object#public_send

def show_block_path
  Rails.application.routes.url_helpers.public_send(
    "#{resource_type.downcase}_path", resource_id
  )
end

最新更新