Ruby Project Setup

C.E.L.T.

--

Going into the last week of Phase 2 of the coding bootcamp, it was a relief to get passed the code challenge and start to see some steps beginning to feel automatic. Remembering syntax and some processes still take time but repetition proved worthwhile when it came to starting an app.

To get an app started with Ruby on Rails, the repetition of the below steps got me through Phase 2:

cd into a folder and create app: rails new story-app

launch server with rails s and got to localhost:3000 on browser: “Yay! You’re on Rails!”

set up models: rails g model user username age:integer location

rails g story title genre user_id:integer user: belongs_to

link relationships between objects:

class User < ApplicationRecord

has_many :stories

end

class Story < ApplicationRecord

belongs_to :user

end

if planning to include validations, add them to the classes:

class User < ApplicationRecord

has_many :stories

validates :username, presence: true

end

class Story < ApplicationRecord

belongs_to :user

validates :title, uniqueness: true

validates :genre, presence: true

end

run migrations and check schema: rails db:migrate

db/schema.rb

seed data by creating user and story instances in seeds.rb and run rails db:seed

verify relationships between class objects in the console: rails c

User.first.stories

Story.last.user

rails s: run the server and go to http://localhost:3000 in browser:

create controllers (include standard ones; later remove those not needed):

rails g controller users index show new create edit update destroy

rails g controller stories index show new create edit update destroy

add routes (resources for all paths, or enter each path individually):

resources :users

get ‘/users/show’, to: ‘users#show’, as: ‘show_user’

resources :stories

get ‘/stories/new’, to: ‘stories#new’, as: ‘new_story’

add view files:

app/views/users/show.html.erb

app/view/stories/new.html.erb

add instance variables to methods in controllers; method names reflect names of html.erb:

def show

@user = User.find(params[:id])

end

def new

@story = Story.new

end

One will read lots of documentation in class and take many notes from lecture videos; these are useful. By actually typing code over and over again, repeating similar steps, muscle memory builds that leads to greater understanding and confidence. May these steps save you some time and get you started faster on your next project.

--

--

No responses yet