Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

created articles model and migrated db #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
class ArticlesController < ApplicationController

def index
@articles = Article.alphabetical.paginate(:page => params[:page]).per_page(10)
end


def show
@article = Article.find(params[:id])
end


def new
@article = Article.new
end


def edit
@article = Article.find(params[:id])
end


def create
@article = Article.new(params[:article])
if @article.save
flash[:notice] = 'Article was successfully created.'
redirect_to article_path(@article)
else
render :action => "new"
end
end


def update
@article = Article.find(params[:id])
if @article.update_attributes(params[:article])
flash[:notice] = 'Article was successfully updated.'
redirect_to article_path(@article)
else
render :action => "edit"
end
end


def destroy
@article = Article.find(params[:id])
@article.destroy
redirect_to articles_path
end
end
8 changes: 8 additions & 0 deletions app/models/article.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Article < ApplicationRecord
belongs_to :category

scope :active, -> { where('active = ?', true) }
scope :alphabetical, -> { order('title') }

validates_presence_of :title, :content
end
15 changes: 15 additions & 0 deletions app/views/articles/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<%= simple_form_for @article, :html => { :class => 'form-horizontal' } do |f| %>
<fieldset>
<legend><%= controller.action_name.capitalize %> Article</legend>

<%= f.input :title %>
<%= f.input :content %>
<%= f.input :category_id, :collection => Category.active.alphabetical, :include_blank => true %>
<%= f.input :active %>

<div class="form-actions">
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to 'Cancel', articles_path, :class => 'btn' %>
</div>
</fieldset>
<% end %>
1 change: 1 addition & 0 deletions app/views/articles/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render :partial => 'form' %>
25 changes: 25 additions & 0 deletions app/views/articles/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<h2>Articles</h2>
<table class="table table-striped">
<thead>
<tr>
<th>Title</th>
<th>Category</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<% @articles.each do |article| %>
<tr>
<td><%= link_to article.title, article_path(article) %></td>
<td><%= article.category.name %></td>
<td>
<%= link_to 'Edit', edit_article_path(article), :class => 'btn btn-mini' %>
<%= link_to 'Destroy', article_path(article), :method => :delete, :confirm => 'Are you sure?', :class => 'btn btn-mini btn-danger' %>
</td>
</tr>
<% end %>
</tbody>
</table>
<%= will_paginate @articles, :previous_label => "Previous&nbsp;", :next_label => "&nbsp;Next" %>

<%= link_to 'New Article', new_article_path, :class => 'btn btn-primary' %>
1 change: 1 addition & 0 deletions app/views/articles/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render :partial => 'form' %>
18 changes: 18 additions & 0 deletions app/views/articles/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<h2><%= @article.title %></h2>

<p>
<%= @article.content %>
</p>

<p>&nbsp;</p>

<p>
<b>Category</b>:
<%= @article.category.name %>
</p>

<div class="form-actions">
<%= link_to 'Back', articles_path, :class => 'btn' %>
<%= link_to 'Edit', edit_article_path(@article), :class => 'btn' %>
<%= link_to 'Delete', article_path(@article), :method => 'delete', :confirm => 'Are you sure?', :class => 'btn btn-danger' %>
</div>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
resources :photos
resources :proverbs
resources :categories
resources :articles
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
12 changes: 12 additions & 0 deletions db/migrate/20180502000700_create_articles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateArticles < ActiveRecord::Migration[5.1]
def change
create_table :articles do |t|
t.string :title
t.text :content
t.integer :category_id
t.boolean :active

t.timestamps
end
end
end
11 changes: 10 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170919031503) do
ActiveRecord::Schema.define(version: 20180502000700) do

create_table "articles", force: :cascade do |t|
t.string "title"
t.text "content"
t.integer "category_id"
t.boolean "active"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "categories", force: :cascade do |t|
t.string "name"
Expand Down
13 changes: 13 additions & 0 deletions test/fixtures/articles.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
title: MyString
content: MyText
category_id: 1
active: false

two:
title: MyString
content: MyText
category_id: 1
active: false
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