How to create Categories in Rails

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

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.

Hello Robert :slight_smile:

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.

On Jul 20, 2013, at 12:56 AM, Giannakis P. [email protected] 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.

On Jul 20, 2013, at 10:21 PM, Giannakis P. [email protected] 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).

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?

:frowning: