# Get parameter from form

**URL:** <https://www.ruby-forum.com/t/get-parameter-from-form/83929>\
**Category:** Rails\
**Created:** [January 30, 2007, 9:22pm UTC](https://www.ruby-forum.com/t/get-parameter-from-form/83929 "2007-01-30T21:22:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jason](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@jason](https://www.ruby-forum.com/u/jason)\
**Post date:** [January 30, 2007, 9:22pm UTC](https://www.ruby-forum.com/t/get-parameter-from-form/83929/1 "2007-01-30T21:22:55Z")

</div>

Hello,

I’m very new at rails. I’m making a simple blog made up of entries. I  
have an entry model and a category model. Obviously, each category will  
have many entries, and each entry will be in a category.

I have a select box that submits a category\_id to my controller. How  
can I use this category\_id to only show entries that have that  
category\_id?

Here’s what I have so far, which just returns nothing:

def list  
@entry\_pages, @entries = paginate :entries, :per\_page =\> 10,  
:order\_by =\> ‘date desc’  
@categories = Category.find\_all  
end

def show\_by\_cat  
cat\_id = params[:entry]  
@entry\_pages, @entries = paginate :entries, :per\_page =\> 10,  
:conditions =\> [“category\_id = ?”,  
cat\_id],  
:order\_by =\> ‘date desc’  
render :action =\> ‘list’  
end

---

<div class="post-metadata">

**Author:** ![jason](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@jason](https://www.ruby-forum.com/u/jason)\
**Post date:** [January 30, 2007, 9:25pm UTC](https://www.ruby-forum.com/t/get-parameter-from-form/83929/2 "2007-01-30T21:25:46Z")

</div>

Here’s the form that generates the select list:

\<% form\_for :entry, :url =\> { :action =\> ‘show\_by\_cat’ } do |form| %\>  
\<%= form.collection\_select(:category\_id, @all\_cats, :id, :name)%\>  
\<%= submit\_tag ‘Go’ %\>  
\<% end %\>

---

<div class="post-metadata">

**Author:** ![jason](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@jason](https://www.ruby-forum.com/u/jason)\
**Post date:** [January 30, 2007, 9:34pm UTC](https://www.ruby-forum.com/t/get-parameter-from-form/83929/3 "2007-01-30T21:34:53Z")

</div>

Try trying changing params[:entry] to params[:entry][:category\_id].

Just to note, it’s probably best to not use form\_for in this instance  
as you are not dealing directly with a model. I would rework this to  
something like this:

\<% form\_tag :action =\> ‘show\_by\_cat’ do %\>  
  
\<%= options\_from\_collection\_for\_select(@all\_cats, :id, :name)%\>  
  
\<%= submit\_tag ‘Go’ %\>  
\<% end %\>

Then in your controller:  
category\_id = params[:category\_id]

Steve

---

<div class="post-metadata">

**Author:** ![jason](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@jason](https://www.ruby-forum.com/u/jason)\
**Post date:** [January 30, 2007, 10:40pm UTC](https://www.ruby-forum.com/t/get-parameter-from-form/83929/4 "2007-01-30T22:40:51Z")

</div>

That worked perfect!

Thanks so much for your help and your advice! I’ll definitely try the  
change you suggested.

Thanks again

Jason
