简体   繁体   中英

Save selected option to variable in rails

I am making a calculator for a product price. I created method "calculator" in Products controller.

My question is: how to save selected option to a variable, which i can use next? In calculator view I put following code:

<%= select("product", "product_id", @products.collect {|p| [ p.title, p.id ] }, {prompt: 'Select Product'}) %>

So, I select product in selector, and I want to save its id to variable. Next, I want to show product price throught this variable: variable.price.

How can I do it?

In controller's action

product_id = params[:product][:product_id]  # save product id to a variable

product_object = Product.find product_id # find product using selected product_id 

product_price = product_object.price 

OR

product_price = (Product.find params[:product][:product_id]).price 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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