Forum: Ruby on Rails Get parameter from form

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
74bd868b70df3590bf94cf7abe563ec4?d=identicon&s=25 Jason (Guest)
on 2007-01-30 21:22

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

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

  def show_by_cat
    cat_id = params[:entry]
    @entry_pages, @entries = paginate :entries, :per_page => 10,
                                      :conditions => ["category_id = ?",
                                      :order_by => 'date desc'
    render :action => 'list'
74bd868b70df3590bf94cf7abe563ec4?d=identicon&s=25 Jason (Guest)
on 2007-01-30 21:25
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 %>
6d57db44c5a4e0721cc7f78cf0ce308a?d=identicon&s=25 Stephen Bartholomew (steveb)
on 2007-01-30 21:34
(Received via mailing list)
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  %>
  <select name="category_id">
   <%= options_from_collection_for_select(@all_cats, :id, :name)%>
  <%= submit_tag 'Go' %>
<% end %>

Then in your controller:
category_id = params[:category_id]

74bd868b70df3590bf94cf7abe563ec4?d=identicon&s=25 Jason (Guest)
on 2007-01-30 22:40
That worked perfect!

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

Thanks again

This topic is locked and can not be replied to.