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
else
@posts = Post.where("name = ?",params[:user_name] )
end
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
field.

I replaced the text input field with
<%= collection_select(:post, :name, Post.select(:name).uniq, :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
accordingly!

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

Alan

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Alan Z. at Feb 2, 2012 at 1:39 pm
    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...
    A

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedFeb 2, '12 at 4:16a
activeFeb 2, '12 at 1:39p
posts2
users1
websiterubyonrails.org
irc#RubyOnRails

1 user in discussion

Alan Z.: 2 posts

People

Translate

site design / logo © 2022 Grokbase