I have a model, Product, and on the show view for this model, I want to have a select box for the user to be able to view a different Product. So, if a user’s looking at the “Product 1” page, they can select “Product 2” from the select box and view that product.
Here’s the code I’m using to create this select box:
<%= form_tag({:controller => "products", :action => "show"}, :method => :get) do %>
<%= select_tag :product_id, options_from_collection_for_select(Product.all, :id, :title) %>
<%= submit_tag 'Go' %>
But this doesn’t work. I can choose a different product, and click Go, but it just goes to the same product, not the one I selected in the box.
What am I doing wrong here?
Basically, I’m trying to use a select box instead of something like this:
<% Product.all.each do |p| %>
<%= link_to p.title, p %>
<% end %>
Here’s what I have for my show action in the controller:
@product = Product.find(params[:id])
rescue ActiveRecord::RecordNotFound
flash[:alert] = 'The product you were looking for could not be found'
redirect_to products_path
well a simple way would be to check if the params[:product_id] was passed by your form, if it is then use that instead of params[:id] basically: