Skip to content

In depth explanation of code steps in README and pseudo code throughout files following steps.

Notifications You must be signed in to change notification settings

srmathison/rails-blog-cheatsheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

How to build a blog in rails guide/cheatsheet

<!-- to read comments clearly make sure text in lower right reads "markdown" -->
<!-- following video tutorial on https://www.youtube.com/watch?v=BI_VnnOLSKY
https://mackenziechild.me/12-in-12/2/ -->

open terminal

cd into where your new blog should go
type..
  <!-- this is the name of the new blog -->
  rails new blog-name 
  <!-- this creates all of the files for blog -->

open sublime

open newly created project
make sure all of the files are there

sanity check

open terminal
type..
  rails s
open chrome
should see the rails guide

open terminal

type..
  <!-- this initializes empty repository -->
  git init
  <!-- make sure all the files are untracked (in red) -->
  git status
  <!-- add files to be tracked -->
  git add .
  <!-- make sure all the files are now tracked (in green) -->
  git status
  <!-- make initial commit -->
  git commit -m "initial commit"

Note..

Think about new blog for a sec...what do you want to be able to do?
      create a user (sign-up)
      login/logout (sign-in/sign-out)
      create posts
      edit/delete posts
      add comments
      admin user ability to delete comments (if spam or unwanted attention)

Note..

Let's start with posts

open terminal

type..
  <!-- this creates the view dir but not the view file -->
  <!-- can type rails g resources posts alternatively as well that will provide a bit more for blog ...checkout guides.rubyonrails.org for reference -->
  rails g controller posts

Note..

Now let's put in some routes

open sublime

open config > routes.rb
type ..
  resources :posts
  <!-- if you want the posts to be in the root of your blog -->
  <!-- root = where it is going... posts = controller... index = action -->
  <!-- for now ignore the do method with nested comments, later when we get to comments you will type resources :comments  -->
  root "posts#index"

Note..

Now you know the PostsController needs the index action

open PostsController

<!-- define index action -->
type..
  def index
  end

Note..

since we generated a controller vs resource we don't have a view > posts > index.html.erb file ...resource does this for you.
Now we have to create the file manually

open app > views > posts

open blank file 
<!-- make sure saves to views > posts -->
save as index.html.erb
type ..
  <!-- filler text for sanity check -->
  this is posts index file
open browser and refresh to check for text on screen

Note..

Next we want the ability to create new posts.

open PostsControlelr

type..
  def new
  end

open browser

<!-- sanity check -->
open /posts/new
<!-- notice we don't have a template, so let's create one -->

create a view >posts new.html.erb file open new.html.erb

create new post form
  <!-- check posts _form.html.erb for code -->
  <!-- create this form in new.html.erb for now, you will see in a few steps when you move it to _form.html.erb and insert a render -->

open terminal <!– Now we have a new post form but not a model to go with it to save our data–>

type..
<!-- creates migration and model with info given -->
rails g model Post title:string body:text
<!-- check files after to confirm -->
<!-- migrate database -->
rake db:migrate

open PostsController

<!-- in order to create new posts have to define create -->
type..
  def create
  <!-- check code -->
  end
  <!-- add private params and check code -->

create posts show.html.erb file

<!-- this is where the post lives -->

open show.html.file

<!-- check code -->

open PostController

<!-- define show -->
type..
def show
<!-- check code -->
end

Note..

sanity check
open browser and create post
any errors review the code

Note..

Now we want to list all of the posts at root of application

edit index action in PostController

<!-- check code -->

edit index.html.erb file

<!-- check code -->
<!-- sanity check -->
<!-- create multiple posts and make sure they display with the info you want -->

open terminal

<!-- if you would like to push to GitHub, create repository now, add remote and push -->
git commit the changes

Note..

If you would like to style what you have so far..
Create a new branch for styling and switch to that branch
<!-- the styling for this example follows the tutorial video I watched -->

open > layout > application.html.erb

create a sidebar
<!-- check code -->
<!-- for now in the sidebar just enter the information for the Blog, root_path. Just type the word "about" in it's own li. We will adjust this later so it makes sense in the process -->
create main content
<!-- check code -->

Note..

for the styling of this tutorial the css/scss will not be pseudo coded. Can do at later point. Imported downloaded css/scss. Checkout the tutorial website listed at top of README for access to original blog with css/normalize. 
<!-- check code on application.scss _normalize.scss -->

open terminal

git commit and merge with master

Note..

We can create posts but can also create a post without entering any info into fields and pushing save post/publish. We don't want to be able to do that. We need validations that will throw errors if a field is left empty. Let's fix that

open models > post.rb

type..
  <!-- check code -->

open posts > new.html.erb

fix post form to list errors
<!-- check code -->
<!-- create posts with missing info to test errors -->

Note..

Now we want to be able to edit/delete our posts.

open PostsController

type..
  def edit
  <!-- check code -->
  end
  def update
  <!-- check code -->
  end

create posts > edit.html.erb create posts > _form.html.erb

this is where you will paste the form you previously created in new.html.erb

open new.html.erb

render the post now that the form is moved
<!-- check code -->

open edit.html.erb

type..
<!-- check code -->

open show.html.erb

add the ability to edit post
<!-- check code -->

Note..

<!-- sanity check -->
try editing and updating posts
Now let's add the ability to delete/destroy a post and a link to destroy it

open PostsController

type..
def destroy
<!-- check code -->
end

open show.html.erb

add link to destroy post
<!-- check code -->

open terminal

git commit changes

Note..

Now we can create a post, edit it, update it, and delete it. Let's create the ability to add a comment. For that we have to create a new model.

open terminal

type..
  rails g model Comment name:string body:text post:references

open models > comment.rb

you can see it created to class with
belongs_to :post
you can see it created the migration file as well

open terminal

<!-- time to migrate! -->
type..
rake db:migrate

Note..

Now we need to add association to the post

open post.rb

type..
  has_many :comments

Note..

Next we need to add routes for our comments

open config > routes.rb

nest the comments in posts
type..
  <!-- check code -->
  <!-- remember the nested comments we ignored a few steps back? -->

Note..

Now let's generate a controller for comments

open terminal

type..
  rails g controller Comments

Note..

Now we have to create a method in our controller to create a new comment

open CommentsController

type..
  def create
  <!-- check code -->
  end

Note..

Next we're going to create a few partials then import them into the post show page

create (in views > comments) the new partial pages

_form.html.erb
_comment.html.erb

open comments > _form.html.erb

type..
  <!-- check code -->

open comments > _comment.html.erb

type..
  <!-- check code -->

open posts > show.html.erb

add comments
<!-- check code -->

Note..

We want the ability to delete comments.

open CommentsController

type..
  def destroy
  <!-- check code -->
  end

open _comment.html.erb

add link to delete comment
<!-- check code -->
<!-- try deleting comments! -->

Note..

We need to make sure that if you delete a post, all of the comments get deleted as well. Otherwise the comments will take up space on the database.

open model > post.rb

add dependent destroy
  <!-- check code -->

open terminal

git commit

Note..

Up until now the about page has been static. We want to create the about page now.

open terminal

type..
  rails g controller pages

open PagesController

<!-- we know we want an about action... -->
def about
end

open config > routes.rb

create route for about
<!-- we don't need full resources like delete/update/destroy -->
<!-- check code -->

Note..

Now we need to create views for about template

open views > pages

create new page about.html.erb

open about.html.erb <!– check code –>

Note..

Now let's take care of that About button we left static a few steps up, right now it is not linking anywhere. At this point if I wanted to view the about page I would have to enter /about in the url.

open layouts > application.html.erb <!– remember that about button without a link? –>

now change the word "about" to a link_to with the erb tag and about path as shown.
<!-- check code -->
add current page if and elsif 
<!-- check code -->

open terminal

git commit

Note..

final thing is add a user. make sure just not anyone can see the posts and make comments.

open Gemfile

add gem 'devise', '~> 3.4.0'

open terminal

bundle install
<!-- if gem is already on system but you want to use devise on your project use the install code in terminal rails g devise:install and follow instructions  -->
rails g devise:views
rails g devise User
rake db:migrate
restart server

Note..

This is the end of my notes for this process. Feel free to experiment with hiding buttons and such whether the user is signed in or not. The video tutuorial lightly touches this subject.

About

In depth explanation of code steps in README and pseudo code throughout files following steps.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published