ROR + 在传递给任何函数之前在控制器中将参数与 Prefix 元素连接起来

ROR + Concatenate params with Prefix element in Controller before passing to any function

在我的 rails 应用程序中,我正在访问一个参数,之后我必须通过将 3 个不同的前缀连接到 3 个不同的函数来传递该参数。但是我无法传递这些值。

示例:Params 变量是 params[:element] 可以是价格或数量。如果 params[:element] 为 nil,则函数调用给出默认选项。但如果价格作为参数,则自动 filter_element 必须分别为 product_price、item_price 和 package_price。如果 qty 作为参数出现,那么 filter_element 必须是 product_qty、item_qty 和 package_qty.

def check_price

  #filter_element = ??? 

  # First Model - Prefix is Product
  first_price = current_user.first_model(filter_element ||= "product_price")

  # Second Model - Prefix is Item
  second_price = current_user.second_model(filter_element ||= "item_price")

  # Third Model - Prefix is Package
  third_price = current_user.third_model(filter_element ||= "package_price")
end

这里我无法将参数与前缀变量连接起来。 提前感谢您的任何建议。

我仍然不确定这个要求,但我想这就是你想要的

def check_price
  # If nil then price otherwise can be either price or qty(assuming)
  suffix = params[:element] || 'price'

  # First Model - Prefix is Product
  first_price = current_user.first_model("product_#{suffix}")

  # Second Model - Prefix is Item
  second_price = current_user.second_model("item_#{suffix}")

  # Third Model - Prefix is Package
  third_price = current_user.third_model("package_#{suffix}")
end