Forum: Ruby on Rails Listing help

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1613d96abb6a9411ad1b3e1ffbaeb074?d=identicon&s=25 Melanie C. (Guest)
on 2006-03-03 23:43
(Received via mailing list)
Hi,

I've created a Rails application and am having a problem listing by a
particular category in an associated table.  I followed the instructions
found at the O'Reilly tutorial
(http://www.onlamp.com/pub/a/onlamp/2005/03/03/rail...) but
have
not had success.  Can anyone please outline a better set of instructions
than in the "Showing Recipes in a Category" section?  Thank you for your
time!

Melanie
7252adadcb6c9e824375c8deaf7174ae?d=identicon&s=25 Nola Stowe (Guest)
on 2006-03-04 06:10
(Received via mailing list)
Hey! ... I saw that .. apparently you are a girl programmer? so am I
.. there seems to be so few of us! I just wondered if perhaps you
wanted to be friends?

I'm 28 and live near Chicago, IL. I've been a programmer for past 8
years. I've been doing PHP post of that time

On 3/3/06, Melanie C. <k_rabbit@hotmail.com> wrote:
> Melanie
>
>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
http://PhpGirl.blogger.com
http://CodeSnipers.com
C59a0cd12b991d3b6266c730dbc4d81e?d=identicon&s=25 Rachel McConnell (Guest)
on 2006-03-04 07:01
(Received via mailing list)
Hi Nola, and other Railschix (to coin a term),

Are you familiar with the Linuxchix lists?  LOTS of girl programmers
there, and in fact girl techies of all sorts.  A lot of times, it's nice
to have a community of *female* experts.  Meaning no offence to anyone
here ;)

Check them out at

   http://www.linuxchix.com/

Rachel
59de94a56fd2c198f33d9515d1c05961?d=identicon&s=25 Tom Mornini (Guest)
on 2006-03-04 07:25
(Received via mailing list)
I find it fascinating that for decades (if not millenia!)
women have been striving for a much deserved equality, and
then now that they're so much closer than they have ever
been to achieving that ideal they perpetuate the grand old
tradition of same sex institutions which have historically
been the ire of their movement in the form of Men's clubs.

I suppose the difference is that it's a choice they've
made, rather than a choice made for them.

In any case, welcome all Rails and Linux Chixs!

--
-- Tom Mornini
C59a0cd12b991d3b6266c730dbc4d81e?d=identicon&s=25 Rachel McConnell (Guest)
on 2006-03-04 08:10
(Received via mailing list)
Sorry for continuing off topic, but I want to correct a misconception I
inadvertently propagated (say that 5 times fast) plus correct a dumb
typo.

Linuxchix isn't women only.

Men are welcome on all but one of the 7 or 8 (I forget exactly) lists -
and the women-only one isn't the heaviest trafficked one by a long shot.

And the real URL is

   http://www.linuxchix.org/

And Tom, thanks for the welcome!  I've already gotten some much-needed
assistance here and hopefully will soon be able to contribute as well.

Rachel


PS- Tom, I have lots of thoughts on same-sex institutions, but I'll keep
that off this list.  Feel free to email me directly if you want to have
that conversation ;)
7252adadcb6c9e824375c8deaf7174ae?d=identicon&s=25 Nola Stowe (Guest)
on 2006-03-04 08:31
(Received via mailing list)
I've seen the linuxchix site and recently started a similar thing
... I started a message board at www.devchix.com .. if interested,
send me an email.



On 3/4/06, Rachel McConnell <rachel@xtreme.com> wrote:
>    http://www.linuxchix.org/
>
> >
> >> nice to have a community of *female* experts.  Meaning  no offence to
> >>> Hey! ... I saw that .. apparently you are a girl programmer? so am I
> >>>> instructions
> > _______________________________________________
> > Rails mailing list
> > Rails@lists.rubyonrails.org
> > http://lists.rubyonrails.org/mailman/listinfo/rails
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
http://PhpGirl.blogger.com
http://CodeSnipers.com
0091f92762685860109bbcb02edfdf27?d=identicon&s=25 Alain Ravet (Guest)
on 2006-03-04 12:06
(Received via mailing list)
Rachel
     >   http://www.linuxchix.org/

I searched for the shop where I could buy the "LinuxChix Wet T-shirt",
but I couldn't find it.
(sorry, I couldn't resist.)


To rant a little, what's next?
       DemocratsRuby.org
       RubyForBlacks
or a Debian distribution for GOP, with weakened encryption and mandatory
backdoors?


In forums and mailing lists, nobody cares if you're ugly, fat or even if
you're a woman, as long as you are polite and don't ask stupid
questions?
When I look around me, women are as rare as they are welcome in the IT
techies world. Good code is good code.

I read  "why linuxchix exists" but I didn't get it.  It must be this
Mars-Venus thing.
I also checked  UbuntuWomen, but I find it even worse.


Alain
6fb7896e1c27e7f15bf335987a37b217?d=identicon&s=25 Aneesha Bakharia (Guest)
on 2006-03-04 12:48
(Received via mailing list)
As a fellow female programmer I just have to say that David Heinemeier
Hansson is very good looking :-)

So there goes all my credability as a female programmer.
C59a0cd12b991d3b6266c730dbc4d81e?d=identicon&s=25 Rachel McConnell (Guest)
on 2006-03-05 00:58
(Received via mailing list)
Everyone else, I won't feed the trolls any more, I promise!

But Alain, and anyone who thinks he might just have a point, please do
your research first.  Start here:

http://www.tldp.org/HOWTO/Encourage-Women-Linux-HOWTO/

Rachel
Eea7ad39737b0dbf3de38874e0a6c7d8?d=identicon&s=25 Justin Forder (Guest)
on 2006-03-05 06:28
(Received via mailing list)
Melanie C. wrote:
> Hi,
>
> I've created a Rails application and am having a problem listing by a
> particular category in an associated table.  I followed the instructions
> found at the O'Reilly tutorial
> (http://www.onlamp.com/pub/a/onlamp/2005/03/03/rail...) but
> have not had success.  Can anyone please outline a better set of
> instructions than in the "Showing Recipes in a Category" section?  Thank
> you for your time!

[Just in case this is what you are stuck on, did you miss the <% end %>
required to match the <% if ... %> added in recipe/list.rhtml? If that's
not the answer, read on.]

Assuming everything was still working at the end of page 2, here's a
breakdown of what is added in "Showing Recipes in a Category":

1) The user needs to be able to choose which category to focus on. This
is enabled by turning the category name, displayed in the list, into a
link. It was

   <td><%= recipe.category.name %></td>

and it becomes

   <%= link_to recipe.category.name,
               :action => "list",
               :category => "#{recipe.category.name}" %>
   </td>

This is a link to the "list" action in the current controller, and it
also provides a "category" parameter whose value is the name of the
category of the recipe.

[I don't know why Curt used "#{recipe.category.name}"
rather than recipe.category.name - either works for me.]

If you *just* make this change to the recipe list.rhtml, the list should
still work, the link should show, and when you hover over it (or view
its source in the browser) you should see a URL like

   http://127.0.0.1:3000/recipe/list?category=Snacks

...but if you click on it, you should still see the *whole* list,
because nothing has been done to capture that parameter and use it to
filter the list.

2) In the RecipeController, the following line is added to the list
method:

     @category = @params['category']

This captures the value of the "category" parameter from the HTTP
request, and puts it in the @category instance variable of the
RecipeController instance handling the request. If there is no
"category" parameter - as when you list all recipes - @category will be
nil.

Rails "magically" makes all the controller's instance variables
available to the view.

The whole of the list method is now:

   def list
     @category = @params['category']
     @recipes = Recipe.find_all
   end

When the list action has executed, Rails will (by default) use a
template of the same name (list.rhtml) to render the response.
As well as putting the category name (or nil) in @category, the action
has put an array of all the recipes in @recipes.

If you make this change, everything should still work, but the list will
still show all the recipes.

[In a real application, which might have thousands of recipes, you would
expect the filtering by category to be done in the database query, but
Curt has kept the original Recipe.find_all and put the filtering in the
view.]

[In the year since this article was published there have been many
changes in Rails, and corresponding changes in recommended programming
style. These days you would write:

   def list
     @category = params['category']
     @recipes = Recipe.find(:all)
   end

...but the old forms still work, for backwards compatibility.]

3) The final change is to filter the list in the view, if @categories is
not nil. In the recipes list.rhtml, the control structure

  <% @recipes.each do |recipe| %>
     ...
  <% end %>

becomes

  <% @recipes.each do |recipe| %>
    <% if (@category == nil) || (@category == recipe.category.name)%>
      ...
    <% end %>
  <% end %>

So a recipe is only displayed as a row in the list if its category name
is the same as @category, or if @category is nil.

Oh - Curt didn't point out in his description that the <% if ... %>
needs a corresponding <% end %> - perhaps that was your problem.

I hope this helps - if not, please tell us more about the symptoms.

   Justin
1613d96abb6a9411ad1b3e1ffbaeb074?d=identicon&s=25 Melanie C. (Guest)
on 2006-03-06 17:54
(Received via mailing list)
Hi,

Wow, thank you for your explanation!  It was very valuable in helping me
find where the problem resides.  The application I've built has many
listing
pages and multiple controllers and at this point I'm outside the realm
of
the scaffolding.  [For example sake, however, I will refer to the
recipes
application.]  When I hover over the category I see that the URL has the
category selected, but when it directs to a new page, all of the recipes
are
listed.

At this point, I think I need help in displaying the single category in
a
new listing page since I'm not using the scaffolding.  Below are two
examples of this.

[Taken from list2, where it selects the category]
     <% @gisrs.each do |gisr| %>
  <% if (@employee == nil) || (@employee == gisr.employee.name)%>
  <tr>
   <td><%= link_to gisr.employee.name, :action => "list3", :employee =>
gisr.employee.name %></td>
<% end %>
<% end %>

[Taken from list3, as stated in the action direction]
<% @gisrs.each do |gisr| %>
   <td><%= gisr.employee.name%></td>
<% end %>

It makes sense to me why it's listing all the "recipes" in list3... I
just
don't know how to change it!  Thanks for your help in this matter - I
appreciate all of your time!
77961c972c437d6801714f45c3f2cc3c?d=identicon&s=25 Raphael Schmid (rapha)
on 2006-03-06 23:31
Nola:
http://PhpGirl.blogger.com seems to be down... at least I can't reach
it...

Aneesha:
Doesn't DHH have a girl friend or even wife already? :-)

- Raphael
Eea7ad39737b0dbf3de38874e0a6c7d8?d=identicon&s=25 Justin Forder (Guest)
on 2006-03-07 01:59
(Received via mailing list)
Melanie C. wrote:
> At this point, I think I need help in displaying the single category in
> <% end %>
>
> [Taken from list3, as stated in the action direction]
> <% @gisrs.each do |gisr| %>
>   <td><%= gisr.employee.name%></td>
> <% end %>
>
> It makes sense to me why it's listing all the "recipes" in list3... I
> just don't know how to change it!

Still following the approach in the cookbook tutorial (i.e. filtering in
the view rather than in the query - not the most efficient approach, but
that may not matter if you don't have too much data or too many
concurrent users) I assume that in the controller, in the list3 action,
you have

     @employee = params['employee']

(or
     @employee = @params['employee']
if you are still using the old style)

Then the filtering needs to be in list3, rather than list2,
i.e. move the

   <% if (@employee == nil) || (@employee == gisr.employee.name)%>
     ...
   <% end %>

into list3.

Does that make sense?

> Thanks for your help in this matter -
> I appreciate all of your time!

You are welcome

   Justin
C3cf03bd3a066fd9cf9846fac671694e?d=identicon&s=25 Andrea Gandino (andre83)
on 2006-03-23 17:27
I'm following the cookbook example on O'Reilly and I've managed to show
all the recipes in a specific category via the list?category=Whatever
url.

I was wondering, is it possible to achieve the very same result with a
so-called Pretty URL, like http://www.site.com/list/Whatever or
www.site.com/Whatever/ ?

Thanks,
Andre.
This topic is locked and can not be replied to.