Skip to content
Permalink
Browse files

Add code from Episode 07

This adds the functionality of viewing a list of consoles and adding new
consoles through TDD.
  • Loading branch information...
brettchalupa committed Nov 26, 2017
1 parent 62cb9f3 commit fb479033ac4c6850e2bd95e1b2b5bad3d83a1c42
@@ -0,0 +1,21 @@
class ConsolesController < ApplicationController
def index
@consoles = Console.all
end

def new
@console = Console.new
end

def create
console = Console.new(console_params)
console.save
redirect_to(consoles_path)
end

private

def console_params
params.require(:console).permit(:name, :manufacturer)
end
end
@@ -0,0 +1,11 @@
<h1>Consoles</h1>

<%= link_to('Add New Console', new_console_path) %>

<ul>
<% @consoles.each do |console| %>
<li>
<%= console.formatted_name %>
</li>
<% end %>
</ul>
@@ -0,0 +1,15 @@
<h1>New Console Form</h1>

<%= form_for(@console) do |f| %>
<label>
Name
<%= f.text_field(:name) %>
</label>

<label>
Manufacturer
<%= f.text_field(:manufacturer) %>
</label>

<%= f.submit('Create') %>
<% end %>
@@ -9,6 +9,10 @@
</head>

<body>
<nav>
<%= link_to('Consoles', consoles_path) %>
</nav>

<%= yield %>
</body>
</html>
@@ -1,6 +1,7 @@
Rails.application.routes.draw do
root(to: 'home#index')
get('about' => 'about#index')
resources(:consoles)

namespace :api do
get('status' => 'status#index')
@@ -0,0 +1,36 @@
require 'rails_helper'

RSpec.describe 'Consoles features' do
let(:view_consoles) do
visit('/')
click_link('Consoles')
end

describe 'viewing the index' do
it 'lists all of the consoles' do
Console.create!(name: 'Switch', manufacturer: 'Nintendo')
Console.create!(name: 'Wii', manufacturer: 'Nintendo')

view_consoles

expect(page).to have_content('Nintendo Switch')
expect(page).to have_content('Nintendo Wii')
end
end

describe 'adding a new console' do
it 'adds the console to the list of consoles' do
view_consoles
click_link('Add New Console')

expect(current_path).to have_content('/consoles/new')

fill_in('Name', with: 'PlayStation Vita')
fill_in('Manufacturer', with: 'Sony')
click_button('Create')

expect(current_path).to have_content('/consoles')
expect(page).to have_content('Sony PlayStation Vita')
end
end
end

0 comments on commit fb47903

Please sign in to comment.
You can’t perform that action at this time.