/rails-review

[rails]

Creative Commons Zero v1.0 UniversalCC0-1.0

Rails Review

$ git clone https://github.com/ga-wdi-exercises/totally-doable
$ cd totally-doable
$ bundle install
$ rails db:create
$ rails db:migrate
$ rails db:seed
Steps completed already ## Initial Setup
$ rails new totally-doable
$ cd totally-doable
$ rails db:create

Database Stuff

$ rails g migration create_todos text completed:boolean
$ rake db:migrate

Create Seeds

# db/seeds.rb

Todo.create([
  {text: 'learn rails', completed: false},
  {text: 'learn javascript', completed: true}
])

Read

Read all

Create a route

# config/routes.rb

get '/todos', to: 'todos#index'

Create a controller

# app/controllers/todos_controller.rb
class TodosController < ApplicationController

end

Create an index method

# app/controllers/todos_controller.rb
class TodosController < ApplicationController
  def index
  end
end

get all the todos from the db

# app/controllers/todos_controller.rb
class TodosController < ApplicationController
  def index
    @todos = Todo.all
  end
end

create a view (template)

<!-- app/views/todos/index.html.erb -->
  <%= @todos.inspect %>

loop through items

<!-- app/views/todos/index.html.erb -->
  <% @todos.each do |todo| %>
    <li>
       <% if todo.completed %>
         <del>
       <% end %>
       <%= todo.text %>
       <% if todo.completed %>
         </del>
       <% end %>
     </li>
  <% end %>

Read a single one

Link to each one in the loop

<%= link_to todo.text, "/todos/#{todo.id}" %>

Create a route in config/routes.rb

Create a method called show

Create a view in app/views/todos/show.html.erb

Create

Update

Delete

Screencasts from a previous review