在传递给任何函数之前,ROR +在Controller中使用Prefix元素连接params

时间:2015-03-18 12:13:29

标签: string ruby-on-rails-4 string-concatenation

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

示例:Params变量是params [:element]可能是price或qty。如果params [:element]为nil,则使用函数调用给出default选项。但是如果价格作为参数出现,则自动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

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

1 个答案:

答案 0 :(得分:1)

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

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
相关问题