Skip to content

Commit

Permalink
Add Articles show and fix fixtures
Browse files Browse the repository at this point in the history
Add the capability to show a single article and fix errors in multiline
fixtures to make controller test assertsions pass.

* Add the Articles show route
* Add the show action to the ArticlesController
* Add the show.html.erb view
* Modify the index view to link to each article
* Fix the fixtures to properly fold multiline text for the Article body

Completes Rails Getting Started Guide:
* 7 CRUDit Where CRUDit Is Due
  * 7.1 Showing a Single Article
  • Loading branch information
msducheminjr committed Dec 23, 2021
1 parent 08201fe commit c2debae
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 4 deletions.
4 changes: 4 additions & 0 deletions app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ class ArticlesController < ApplicationController
def index
@articles = Article.all
end

def show
@article = Article.find(params[:id])
end
end
4 changes: 3 additions & 1 deletion app/views/articles/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
<ul>
<% @articles.each do |article| %>
<li>
<%= article.title %>
<a href="/articles/<%= article.id %>">
<%= article.title %>
</a>
</li>
<% end %>
</ul>
3 changes: 3 additions & 0 deletions app/views/articles/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1><%= @article.title %></h1>

<p><%= @article.body %></p>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
root "articles#index"

get "/articles", to: "articles#index"
get "/articles/:id", to: "articles#show"
end
11 changes: 11 additions & 0 deletions test/controllers/articles_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
require "test_helper"

class ArticlesControllerTest < ActionDispatch::IntegrationTest
def setup
@article = articles(:nerd)
end

test "should get index" do
get articles_url
assert_response :success
assert_select 'h1', 'Articles'
assert_select 'li', Article.count
end

test "should get show" do
get "/articles/#{@article.id}"
assert_response :success
assert_select 'h1', @article.title
assert_select 'p', @article.body
end
end
5 changes: 2 additions & 3 deletions test/fixtures/articles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

nerd:
title: Check out NerdDice
body: |
body: >-
These videos take you end-to-end through a project that creates a ruby gem.
(The subject matter of polyhedral dice was chosen because Mike\'s a giant nerd.)
The series covers a number of programming concepts that will help you be a better
software developer, particularly in Ruby.
why:
title: Why Stateless Code
body: |
body: >-
Inspired by Simon Sinek\'s brilliant video Start with Why, these videos explain the
North Star of Stateless Code. What gets us out of bed in the morning and motivates
us to keep producing videos? Why do we do what we do? What is the dream for this
Expand Down

0 comments on commit c2debae

Please sign in to comment.