Guest User

Untitled

a guest
May 21st, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. adding faker:
  2. add faker gem to Gemfile "gem 'faker'"
  3. ****everytime you change the Gemfile, you must bundle install**** ('bundle install' in terminal)
  4. add 'Faker' within the seeds.rb tab
  5. ie: email: Faker::Internet.free_email("#{first_name}, #{last_name}"),
  6. rake db:seed in terminal
  7.  
  8. post verbs: post, patch, delete (change, but not display) (these are the ones that view's are redirected to show/index/etc)
  9.  
  10. Flash Hash
  11.  
  12. Step 1 (in application.html.erb)
  13. <% flash.each do |name, message| %>
  14. <%= message %>
  15. <% end %>
  16.  
  17. Step 2 (in controller)
  18. flash[:success] = "Successfully Updated Recipe"
  19.  
  20. Flash messages are only viewed ON THE NEXT HTML RENDER. If you refresh the page, the flash message will be gone. (used in create, update, and destroy)
  21.  
  22. Change/Update Database Model without destroying it (MIGRATION)
  23.  
  24. 1) create new rails migration app
  25. 2) go to db-migrate-jibberishfilename and check model is what you were expecting. Check input types (string, integer, etc)
  26. (once you make this page, you can edit it until you 'rake:db migrate' - once it is in the database you DO NOT edit this page, otherwise you could blow everything up)
  27. Edit Database after it has been migrated:
  28. in terminal: "rails g migration AddPriceToProducts" (this will make the file add_price_to_products.rb in the migrate folder)
  29. ^^ this will add a new migration file to the 'migrate' folder
  30. open the file and add to the method "change"
  31. example: add_column :products, :price, :integer
  32. ^^ name of table, name of attribute(column), name of data type
  33. rake db:migrate - this will add in the changes to the database
  34. (note: this will update schema.rb file) (reminder: never touch schema file, to make changes you need migration files)
  35.  
  36. rake db:create - adds folders to empty file cabinet
  37. rake g migration - creates new file that describes folders
  38. rake db:migreate - translates ruby code into sequel and enters the database
  39. seeds.rb - static ruby file with all information - in order to run this static file into SQL we run -->
  40. 'rake db:seed' - this makes the ruby data into the structure of the file cabinet (SQL)
  41. **You must have structure of database before migrating**
  42.  
  43. SHORTCUT FOR MIGRATING (IN TERMINAL)
  44. "rails g migration AddStockToProducts stock: integer"
  45. ^^ this will create a new migration file with a new column for Stock in table Products with data type Integer - review and then rake db:migrate
  46. "rails g migration RemoveStockFromProducts stock: integer"
  47. ^^ This will create a new migration file which will delete the Stock column from the Products table, with data type integer
  48.  
  49. db:migrate has 3 possible outcomes: error, success, literally nothing
  50.  
  51. schema.rb - report of current structure of database
  52.  
  53. COLUMN TYPES
  54. string - limited to 255 characters
  55. text - unlimited characters
  56. integer - whole numbers
  57. float - not an available data type for postgres, instead they use:
  58. decimals - exact, instead of having .28 *100= 28.00000000000004
  59. boolean - use to store true/false values
  60. binary - used to stroe images, movies, and other files in their original, raw format - any media (heroku can't store binary files) (stick to URLs)
  61. :primary_key -
  62. date -
  63. time -
  64. DateTime -
  65. Timestamp -
Add Comment
Please, Sign In to add comment