Skip to content

Commit

Permalink
Generate the Article model and add to index view
Browse files Browse the repository at this point in the history
Add an Article model to match the previously created ArticlesController

* Generate the Article model with title:string and body:text attributes.
* Run the generated migration to produce the schema.rb file.
* Change the default generated fixtures to be less boring
* Modify the articles index action and view to query and display a list
  of articles
* Update the ArticlesControllerTest with new functionality

Completes Rails Getting Started Guide:
* 6 MVC and You
  * 6.1 Generating a Model
  * 6.2 Database Migrations
  * 6.3 Using a Model to Interact with the Database
  * 6.4 Showing a List of Articles
  • Loading branch information
msducheminjr committed Dec 22, 2021
1 parent a1be17b commit 08201fe
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class ArticlesController < ApplicationController
def index
@articles = Article.all
end
end
2 changes: 2 additions & 0 deletions app/models/article.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Article < ApplicationRecord
end
10 changes: 9 additions & 1 deletion app/views/articles/index.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
<h1>Hello, Rails! Taxation is theft!</h1>
<h1>Articles</h1>

<ul>
<% @articles.each do |article| %>
<li>
<%= article.title %>
</li>
<% end %>
</ul>
10 changes: 10 additions & 0 deletions db/migrate/20211221193413_create_articles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateArticles < ActiveRecord::Migration[7.0]
def change
create_table :articles do |t|
t.string :title
t.text :body

t.timestamps
end
end
end
22 changes: 22 additions & 0 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion test/controllers/articles_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class ArticlesControllerTest < ActionDispatch::IntegrationTest
test "should get index" do
get articles_url
assert_response :success
assert_select 'h1', 'Hello, Rails! Taxation is theft!'
assert_select 'h1', 'Articles'
assert_select 'li', Article.count
end
end
22 changes: 22 additions & 0 deletions test/fixtures/articles.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

nerd:
title: Check out NerdDice
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: |
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
organization? Start at the center of the Golden Circle and communicate outward rather
than on the outside and communicating inward. Are we just being lazy video editors by
leaving the mistakes in our videos? Why the Jesus is Lord in the footer and the
in-your-face anarchy symbol in the branding? These videos are here to remind ourselves
of our promise to you. Taking the time to stop and think about the Why will help you
prioritize and avoid mission creep.
7 changes: 7 additions & 0 deletions test/models/article_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class ArticleTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit 08201fe

Please sign in to comment.