Get parameter from form

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

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 %>

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

That worked perfect!

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

Thanks again

Jason