Skip to content

Instantly share code, notes, and snippets.

@kattak
Last active June 30, 2017 01:11
Show Gist options
  • Save kattak/621d4cb96a77d77beab88d88135d8e14 to your computer and use it in GitHub Desktop.
Save kattak/621d4cb96a77d77beab88d88135d8e14 to your computer and use it in GitHub Desktop.
(in progress)

Make a Rails blog in 10 minutes

New Rails App with postgres

rails new APPNAME --database=postgresql

Why can't we use the default (sqlite) database?

  • Heroku is a popular free hosting service.
  • Heroku doesn’t use SQLite3, which is the default database in new Rails apps.
    • Why is SQLite incompatible with Heroku?
    • SQLite stores the database to a file in your db/ directory.
    • PGSQL

-----
### Scaffold post object
  • This creates a blog post object with a title and a body.
  • The scaffold will generate a lot of files - I've specified the ones I don't want.
  • I can always create these files later if needed.
rails g scaffold post title:string text:text --no-assets —no-test-framework --no-helper —no-json

A scaffold will generate the following files:

  • Models
  • Views
  • Controllers
  • Tests
  • Assets
  • Helpers
  • Routes
  • Active Record Migration File

Pretend Command

The -p pretend command shows me what files would be created without actually creating them.

rails g scaffold post title:string text:text --no-assets —no-test-framework --no-helper —no-json
 -p

Make model only

rails g model comment post_id:integer text:text
  • Makes model, migration file, and test_unit (2 test files)

Database commands

rake db:migrate

Run Rails Server

rails s 

rails g controller comments create destroy #creates routes, views, helper, assets, scss, test_unit(2)

================================ To run migration : rails g migration create_posts -p *call with create_posts. not just posts *only creates the migration file, no helpers, models, etc.

================================ scaffold creates: AR — create_posts

Model post.rb

Test Unit For model, controller Resource resources :posts in ApplicationController create posts_controller

ERB For posts, index, edit, show, new, _form


Resources & Advice:


@kattak
Copy link
Author

kattak commented Jan 11, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment