0

I'm looking for the best/most efficient way to add categories that save to a database. I've created the category model with a name column. I generated the controller. My first thought was to just enter the categories directly into the controller since I will be the only one that can create, edit, and destroy them. This is what it looked like:

class CategoriesController < ApplicationController

def women 
end

def kids
end

def babies
end

def home_decor
end

end

Fine, but that doesn't save to the db and since I'd like to associate these categories w/ products later I need it to save to the db. I could create these categories directly in console, but I'm not sure if when I push to production (on Heroku) how to create them again (and that seems a little tedious).

The other option I have is to create a form to create the categories and only give access to an admin.

Am I missing something? Is there a more efficient way to do this or is the admin route the best way to handle it?

Thanks for any suggestions!

Kelly
  • 469
  • 1
  • 7
  • 20

1 Answers1

2

Forget that controller. What you are looking for is in db/seeds.rb. It's just a script where you place the default content of your database. So in your case you'll need to put the following:

Category.delete_all # For avoiding duplicate content

Category.create!({id: 1, name: 'women'}) # Use create! so you'll know if there is any errors
Category.create!({id: 2, name: 'kids'})
# etc...

Then just run the script for placing that content in the db:

rake db:seed

And in heroku:

heroku run rake db:seed
Mario Pérez Alarcón
  • 3,468
  • 2
  • 27
  • 38
  • Thanks Mario. It worked great on dev, but I'm having problems w/ it on heroku due to another error. Is there a way to view the Heroku database, maybe something similar to the sqlite browser? – Kelly Nov 12 '14 at 18:23
  • What problem exactly? You could run `heroku run rails console`. Did you migrate your database on heroku? – Mario Pérez Alarcón Nov 12 '14 at 18:29
  • I get this error message 'rake aborted! ActiveRecord::UnknownAttributeError: unknown attribute: admin' I had a user in seeds.db that had admin status but I had not created that table in the db for users. So I created the table for admin and still got the error message. I reset the db too and tried the heroku run rake db:seed again and same thing. I then tried to delete that user from my seeds.db and reset but still getting same error message. Thanks for your help! – Kelly Nov 12 '14 at 18:33
  • You're welcome! Try with `heroku run rake db:migrate` to see if it helps – Mario Pérez Alarcón Nov 12 '14 at 18:36
  • heroku run rake db:migrate works fine, but after when I try to do heroku run rake db:seed I get same error message. When i do run rake db:migrate, does it include the seed or are they separate? Thanks! – Kelly Nov 12 '14 at 18:41
  • They're different. This [answer](http://stackoverflow.com/a/10302357/2273578) sums up what you need to know about running those commands. I need more info about your schema and/or models to see what is going on... – Mario Pérez Alarcón Nov 12 '14 at 18:47
  • Thanks again. I just added the schema to the original question. I appreciate the help. If there is any other model info you need please let me know. This is so frustrating. – Kelly Nov 12 '14 at 19:16
  • Well, it works now. Not sure what I did differently, but it's working. Thanks again for all the help! – Kelly Nov 12 '14 at 19:32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/64813/discussion-between-mario-perez-and-kelly). – Mario Pérez Alarcón Nov 12 '14 at 20:07