diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb new file mode 100755 index 0000000..da1559f --- /dev/null +++ b/app/controllers/articles_controller.rb @@ -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 diff --git a/app/models/article.rb b/app/models/article.rb new file mode 100644 index 0000000..c177aea --- /dev/null +++ b/app/models/article.rb @@ -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 diff --git a/app/views/articles/_form.html.erb b/app/views/articles/_form.html.erb new file mode 100755 index 0000000..1377666 --- /dev/null +++ b/app/views/articles/_form.html.erb @@ -0,0 +1,15 @@ +<%= simple_form_for @article, :html => { :class => 'form-horizontal' } do |f| %> +
+ <%= controller.action_name.capitalize %> Article + + <%= f.input :title %> + <%= f.input :content %> + <%= f.input :category_id, :collection => Category.active.alphabetical, :include_blank => true %> + <%= f.input :active %> + +
+ <%= f.submit nil, :class => 'btn btn-primary' %> + <%= link_to 'Cancel', articles_path, :class => 'btn' %> +
+
+<% end %> diff --git a/app/views/articles/edit.html.erb b/app/views/articles/edit.html.erb new file mode 100755 index 0000000..786950e --- /dev/null +++ b/app/views/articles/edit.html.erb @@ -0,0 +1 @@ +<%= render :partial => 'form' %> diff --git a/app/views/articles/index.html.erb b/app/views/articles/index.html.erb new file mode 100755 index 0000000..0aaa224 --- /dev/null +++ b/app/views/articles/index.html.erb @@ -0,0 +1,25 @@ +

Articles

+ + + + + + + + + + <% @articles.each do |article| %> + + + + + + <% end %> + +
TitleCategoryActions
<%= link_to article.title, article_path(article) %><%= article.category.name %> + <%= 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' %> +
+<%= will_paginate @articles, :previous_label => "Previous ", :next_label => " Next" %> + +<%= link_to 'New Article', new_article_path, :class => 'btn btn-primary' %> diff --git a/app/views/articles/new.html.erb b/app/views/articles/new.html.erb new file mode 100755 index 0000000..786950e --- /dev/null +++ b/app/views/articles/new.html.erb @@ -0,0 +1 @@ +<%= render :partial => 'form' %> diff --git a/app/views/articles/show.html.erb b/app/views/articles/show.html.erb new file mode 100755 index 0000000..9f07e38 --- /dev/null +++ b/app/views/articles/show.html.erb @@ -0,0 +1,18 @@ +

<%= @article.title %>

+ +

+ <%= @article.content %> +

+ +

 

+ +

+ Category: + <%= @article.category.name %> +

+ +
+ <%= 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' %> +
diff --git a/config/routes.rb b/config/routes.rb index 8c215d3..9ecfa33 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/db/migrate/20180502000700_create_articles.rb b/db/migrate/20180502000700_create_articles.rb new file mode 100644 index 0000000..833625a --- /dev/null +++ b/db/migrate/20180502000700_create_articles.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 31f186b..3718286 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" diff --git a/test/fixtures/articles.yml b/test/fixtures/articles.yml new file mode 100644 index 0000000..7e12f89 --- /dev/null +++ b/test/fixtures/articles.yml @@ -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 diff --git a/test/models/article_test.rb b/test/models/article_test.rb new file mode 100644 index 0000000..11c8abe --- /dev/null +++ b/test/models/article_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class ArticleTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end