Reputation: 145
I'm trying to use a collection select and from there go to my user_show page, but I can't seem to figure out how to send the selected variable through to be displayed.
Here is my current code:
<%= form_tag user_path(:id), :method => :post do %>
<%= collection_select(:user, :id, User.all, :id, :name) %>
<button type="submit">Sign In</button>
<% end %>
This is the closest I've gotten, but it is reading the :id as id. What is the correct way to do this?
Upvotes: 1
Views: 486
Reputation: 34336
Your collection_select should be like this (with explanation of each field):
collection_select(
:user, # field namespace
:user_id, # field name
# result of these two params will be: <select name="user[user_id]">
# then you should specify some collection or array of rows.
# In your example it is:
User.all,
# then you should specify methods for generating options
:id, # this is name of method that will be called for every row, result will be set as key
:name, # this is name of method that will be called for every row, result will be set as value
# as a result, every option will be generated by the following rule:
# 'user' is an element in the collection or array
)
So, change your collection_select
to this:
<%= collection_select(:user, :user_id, User.all, :id, :name) %>
Upvotes: 2
Reputation: 7655
You probably need:
<%= form_tag user_path(:id), :method => :post do %>
<%= collection_select(:user, :user_id, User.all, :id, :name) %>
<button type="submit">Sign In</button>
<% end %>
Upvotes: 1