Using collection_select to pass arbitrarily named field

I am a newbie to RoR and am trying to learn Rails by developing some
simple applications, following various tutorials. I hope someone can
help me over the following bump in the road.

I developed a page that lists a collection of “posts”, displaying the
names of those who posted. On this page I would like to place an input
field that “filters” the results when the page is resubmitted.

I had no trouble adding
<%= form_tag({:controller => “posts”, :action => “index”}, :method =>
“get”) do %>
<%= submit_tag(“Apply Name Filter:”) %>
<%= text_field_tag(:user_name) %>
<% end %>
to the view and adding
if params[:user_name] == nil or params[:user_name] == ‘*’
@posts = Post.all
@posts = Post.where(“name = ?”,params[:user_name] )
to the controller-- but this is not what I want.

I want to add a select input field that lists all the names of all the
users in the system so the user can simply choose the name to use as a
filter. The list of all names may not be the same as the collection
passed to the view – especially if I ever get this filter working!! –
so I want to fetch them all from the database when populating the select

I replaced the text input field with
<%= collection_select(:post, :name,, :name,
:name) %>
and indeed generated a select list of unique names… but I do not know
how to code the controller test. Indeed, I end up with a parameter named
“post[name]” but cannot test that. Although the filter options are the
collection of names, the select is not really tied to an object instance
at all, and I would just name it “user_name” as I did with the text
input field, if only I knew how to code the collection_select

As you can see I am confused about the best way to do this. All
suggestions are appreciated.


I figured it out… my mistake was simply not accessing the nested hash
correctly as params[“post”][“name”] – and not testing for the case
where it was null.
Sorry for the noise…

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs