Forum: Ruby on Rails How to create Categories in Rails

541180328ee026805be8d4263103763c?d=identicon&s=25 Giannakis P. (giannakis_p)
on 2013-07-20 09:56
Hey Ruby on Rails Community

i'm trying to Add Categories to my Rails app, but don't quite know how
to do this.

I have many Pins(Images) and want the user to be able to assign a
category on those Pins. ASSIGN not create, edit, or delete a Category,
just selecting one for their Pin. Meaning that, When a user uploads a
pin, he can choose from a dropdown list a Category.

Then, another user can choose from the Menu a Category, and ONLY the
Pins in this Category will be listed.

How do i do this? Where to start ?

Thank you
280b78a61a968391b7e07e912be102a8?d=identicon&s=25 Robert Walker (robert4723)
on 2013-07-20 19:48
Giannakis P. wrote in post #1116088:
> Hey Ruby on Rails Community
>
> i'm trying to Add Categories to my Rails app, but don't quite know how
> to do this.
>
> I have many Pins(Images) and want the user to be able to assign a
> category on those Pins. ASSIGN not create, edit, or delete a Category,
> just selecting one for their Pin. Meaning that, When a user uploads a
> pin, he can choose from a dropdown list a Category.
>
> Then, another user can choose from the Menu a Category, and ONLY the
> Pins in this Category will be listed.
>
> How do i do this? Where to start ?

Sounds to me like a tagging system such as
https://github.com/mbleigh/acts-as-taggable-on would work well for your
needs.
541180328ee026805be8d4263103763c?d=identicon&s=25 Giannakis P. (giannakis_p)
on 2013-07-21 07:21
Hello Robert :)

No no, i need a clear category table. Here's what i done

I generated a migration

class AddCategoriesToPins < ActiveRecord::Migration
  def change
    add_column :pins, :category, :string
  end
end

in my Pin Uploading form i added

<%= f.select :category, [ 'Box', 'Cover', 'Poster' ], :prompt => 'Select
One' %>

Now, the Selecting a Category when uploading Works, and the category
shows up properly in the Database.

But i want the user to click on that category in the Show Page and see
all the pin's that are in that category.

P.S.: Is this method even a good one ? I dont want the user's to be able
to create, edit, or delete a Category. Just selecting it.
Aa082c8b00a50928e5860dcd70bf2368?d=identicon&s=25 tamouse m. (tamouse_m)
on 2013-07-21 08:33
(Received via mailing list)
On Jul 20, 2013, at 12:56 AM, Giannakis P. <lists@ruby-forum.com> wrote:

> Then, another user can choose from the Menu a Category, and ONLY the
> Pins in this Category will be listed.
>
> How do i do this? Where to start ?
>
> Thank you


It sounds as though you are going to limit them to one category per pin.

Simply using that, a given pin will belong to a category, and a category
will have many pins. This should tell you the relationship to use. Read
the rails guide on relationships for how to set this up.

Since you are not allowing the user to define categories, you will need,
as an admin or as part of your app configuration, to fill in the
categories you want your users to be able to select from.
Aa082c8b00a50928e5860dcd70bf2368?d=identicon&s=25 tamouse m. (tamouse_m)
on 2013-07-21 09:00
(Received via mailing list)
On Jul 20, 2013, at 10:21 PM, Giannakis P. <lists@ruby-forum.com> wrote:

> end
> all the pin's that are in that category.
>
> P.S.: Is this method even a good one ? I dont want the user's to be able
> to create, edit, or delete a Category. Just selecting it.


This can certainly work, though seems less flexible in the long run as
to update categories you have to go into the code base and make the
change there, rather than have a configurable set in a table.

But even with this, it's quite possible, but again, you're necessarily
hard coding the selection instead of drawing it from a table. In you
category navigation, you offer the user the ability to select on of Box,
Cover or Poster, and then use gather them in the appropriate controller
with pins = Pin.where(:category => params[:category]) for example
(depending on how you actually get the user's desired category).
541180328ee026805be8d4263103763c?d=identicon&s=25 Giannakis P. (giannakis_p)
on 2013-07-21 09:38
It seems like the best way to do this is to generate a Categories
scaffold and allowing only admins to create, update or Delete the
Categories.

This is the Easy Part, i get this.

But now, how do i get the selection dropdown in to my form, allowing
users to choose a category?

:(
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.