From 76f2e2001f9ec1d15d41f8dbf25379fbbab1d1fa Mon Sep 17 00:00:00 2001 From: Yang Wen Date: Thu, 1 Sep 2016 22:04:51 +0800 Subject: [PATCH 01/24] add article link to nav bar --- app/views/shared/_nav.html.erb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/views/shared/_nav.html.erb b/app/views/shared/_nav.html.erb index c6ea2b4..27d55c2 100644 --- a/app/views/shared/_nav.html.erb +++ b/app/views/shared/_nav.html.erb @@ -41,6 +41,7 @@
  • + <%= link_to('Articles', articles_path ) %>
  • <% if user_signed_in? %>
  • From d5968287a8df6962b3c8a6ca094d19f53023dde4 Mon Sep 17 00:00:00 2001 From: Yang Wen Date: Thu, 1 Sep 2016 22:05:18 +0800 Subject: [PATCH 02/24] add resouces :articles to routes.rb --- config/routes.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/config/routes.rb b/config/routes.rb index faaec11..64c45b6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,4 +2,5 @@ devise_for :users # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html root to: 'visitors#index' + resources :articles end From f0461e4360c3451cb61ea4a556d72ecce24990e9 Mon Sep 17 00:00:00 2001 From: Yang Wen Date: Thu, 1 Sep 2016 22:05:38 +0800 Subject: [PATCH 03/24] create controller articles --- app/assets/javascripts/articles.coffee | 3 +++ app/assets/stylesheets/articles.scss | 3 +++ app/controllers/articles_controller.rb | 2 ++ app/helpers/articles_helper.rb | 2 ++ test/controllers/articles_controller_test.rb | 7 +++++++ 5 files changed, 17 insertions(+) create mode 100644 app/assets/javascripts/articles.coffee create mode 100644 app/assets/stylesheets/articles.scss create mode 100644 app/controllers/articles_controller.rb create mode 100644 app/helpers/articles_helper.rb create mode 100644 test/controllers/articles_controller_test.rb diff --git a/app/assets/javascripts/articles.coffee b/app/assets/javascripts/articles.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/articles.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/articles.scss b/app/assets/stylesheets/articles.scss new file mode 100644 index 0000000..cca5487 --- /dev/null +++ b/app/assets/stylesheets/articles.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the articles controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb new file mode 100644 index 0000000..2cab2c8 --- /dev/null +++ b/app/controllers/articles_controller.rb @@ -0,0 +1,2 @@ +class ArticlesController < ApplicationController +end diff --git a/app/helpers/articles_helper.rb b/app/helpers/articles_helper.rb new file mode 100644 index 0000000..2968277 --- /dev/null +++ b/app/helpers/articles_helper.rb @@ -0,0 +1,2 @@ +module ArticlesHelper +end diff --git a/test/controllers/articles_controller_test.rb b/test/controllers/articles_controller_test.rb new file mode 100644 index 0000000..aae3248 --- /dev/null +++ b/test/controllers/articles_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class ArticlesControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end From ba610bce0ab951f59edc292fa60f65d203e6f774 Mon Sep 17 00:00:00 2001 From: Yang Wen Date: Thu, 1 Sep 2016 22:11:49 +0800 Subject: [PATCH 04/24] creat controller articles --- app/controllers/articles_controller.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index 2cab2c8..a5d9666 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -1,2 +1,5 @@ class ArticlesController < ApplicationController + def index + + end end From 700704692ff4ecfdd664cf17b133dc337aac1378 Mon Sep 17 00:00:00 2001 From: Yang Wen Date: Thu, 1 Sep 2016 22:13:24 +0800 Subject: [PATCH 05/24] create model Article --- app/models/article.rb | 4 + app/views/articles/index.html.erb | 160 +++++++++++++++++++ db/migrate/20160901141023_create_articles.rb | 13 ++ test/fixtures/articles.yml | 15 ++ test/models/article_test.rb | 7 + 5 files changed, 199 insertions(+) create mode 100644 app/models/article.rb create mode 100644 app/views/articles/index.html.erb create mode 100644 db/migrate/20160901141023_create_articles.rb create mode 100644 test/fixtures/articles.yml create mode 100644 test/models/article_test.rb diff --git a/app/models/article.rb b/app/models/article.rb new file mode 100644 index 0000000..9c5f63c --- /dev/null +++ b/app/models/article.rb @@ -0,0 +1,4 @@ +class Article < ApplicationRecord + belongs_to :category + belongs_to :user +end diff --git a/app/views/articles/index.html.erb b/app/views/articles/index.html.erb new file mode 100644 index 0000000..4925326 --- /dev/null +++ b/app/views/articles/index.html.erb @@ -0,0 +1,160 @@ +
    + + +
    +
    +

    Blog Home One + Subheading +

    + +
    +
    + + +
    + + +
    + + +

    + Blog Post Title +

    +

    + by Start Bootstrap +

    +

    Posted on August 28, 2013 at 10:00 PM

    +
    + + + +
    +

    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolore, veritatis, tempora, necessitatibus inventore nisi quam quia repellat ut tempore laborum possimus eum dicta id animi corrupti debitis ipsum officiis rerum.

    + Read More + +
    + + +

    + Blog Post Title +

    +

    + by Start Bootstrap +

    +

    Posted on August 28, 2013 at 10:45 PM

    +
    + + + +
    +

    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quibusdam, quasi, fugiat, asperiores harum voluptatum tenetur a possimus nesciunt quod accusamus saepe tempora ipsam distinctio minima dolorum perferendis labore impedit voluptates!

    + Read More + +
    + + +

    + Blog Post Title +

    +

    + by Start Bootstrap +

    +

    Posted on August 28, 2013 at 10:45 PM

    +
    + + + +
    +

    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Cupiditate, voluptates, voluptas dolore ipsam cumque quam veniam accusantium laudantium adipisci architecto itaque dicta aperiam maiores provident id incidunt autem. Magni, ratione.

    + Read More + +
    + + + + +
    + + +
    + + +
    +

    Blog Search

    +
    + + + + +
    + +
    + + +
    +

    Blog Categories

    + + +
    + + +
    +

    Side Widget Well

    +

    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Inventore, perspiciatis adipisci accusamus laudantium odit aliquam repellat tempore quos aspernatur vero.

    +
    + +
    + +
    + + +
    + + +
    +
    +
    +

    Copyright © Your Website 2014

    +
    +
    +
    + +
    \ No newline at end of file diff --git a/db/migrate/20160901141023_create_articles.rb b/db/migrate/20160901141023_create_articles.rb new file mode 100644 index 0000000..f513e41 --- /dev/null +++ b/db/migrate/20160901141023_create_articles.rb @@ -0,0 +1,13 @@ +class CreateArticles < ActiveRecord::Migration[5.0] + def change + create_table :articles do |t| + t.string :title + t.text :cotent + t.boolean :is_public + t.references :category, foreign_key: true + t.belongs_to :user, foreign_key: true + + t.timestamps + end + end +end diff --git a/test/fixtures/articles.yml b/test/fixtures/articles.yml new file mode 100644 index 0000000..9e5d4f8 --- /dev/null +++ b/test/fixtures/articles.yml @@ -0,0 +1,15 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + title: MyString + cotent: MyText + is_public: false + category: one + user: one + +two: + title: MyString + cotent: MyText + is_public: false + category: two + user: two 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 From 0fece7bcd733e8079444afb89cba8e7c00bfcf65 Mon Sep 17 00:00:00 2001 From: Yang Wen Date: Thu, 1 Sep 2016 22:14:25 +0800 Subject: [PATCH 06/24] create model Category and db:migrate --- app/models/category.rb | 2 ++ .../20160901141339_create_categories.rb | 9 +++++++++ db/schema.rb | 20 ++++++++++++++++++- test/fixtures/categories.yml | 7 +++++++ test/models/category_test.rb | 7 +++++++ 5 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 app/models/category.rb create mode 100644 db/migrate/20160901141339_create_categories.rb create mode 100644 test/fixtures/categories.yml create mode 100644 test/models/category_test.rb diff --git a/app/models/category.rb b/app/models/category.rb new file mode 100644 index 0000000..54cb6ae --- /dev/null +++ b/app/models/category.rb @@ -0,0 +1,2 @@ +class Category < ApplicationRecord +end diff --git a/db/migrate/20160901141339_create_categories.rb b/db/migrate/20160901141339_create_categories.rb new file mode 100644 index 0000000..4d7134a --- /dev/null +++ b/db/migrate/20160901141339_create_categories.rb @@ -0,0 +1,9 @@ +class CreateCategories < ActiveRecord::Migration[5.0] + def change + create_table :categories do |t| + t.string :name + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index e5c8535..018798e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,25 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160901134415) do +ActiveRecord::Schema.define(version: 20160901141052) do + + create_table "articles", force: :cascade do |t| + t.string "title" + t.text "cotent" + t.boolean "is_public" + t.integer "category_id" + t.integer "user_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["category_id"], name: "index_articles_on_category_id" + t.index ["user_id"], name: "index_articles_on_user_id" + end + + create_table "categories", force: :cascade do |t| + t.string "name" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end create_table "users", force: :cascade do |t| t.string "email", default: "", null: false diff --git a/test/fixtures/categories.yml b/test/fixtures/categories.yml new file mode 100644 index 0000000..56066c6 --- /dev/null +++ b/test/fixtures/categories.yml @@ -0,0 +1,7 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + name: MyString + +two: + name: MyString diff --git a/test/models/category_test.rb b/test/models/category_test.rb new file mode 100644 index 0000000..4733541 --- /dev/null +++ b/test/models/category_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class CategoryTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end From b788f42537903e7cc93c53fc50c54c78ebeaabf7 Mon Sep 17 00:00:00 2001 From: Yang Wen Date: Thu, 1 Sep 2016 22:19:23 +0800 Subject: [PATCH 07/24] =?UTF-8?q?=E5=85=B3=E8=81=94=E6=A8=A1=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/category.rb | 1 + app/models/user.rb | 1 + 2 files changed, 2 insertions(+) diff --git a/app/models/category.rb b/app/models/category.rb index 54cb6ae..419c22f 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -1,2 +1,3 @@ class Category < ApplicationRecord + has_many :articles end diff --git a/app/models/user.rb b/app/models/user.rb index b2091f9..c3c343b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -3,4 +3,5 @@ class User < ApplicationRecord # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable + has_many :articles end From 8a2acdf32237af259f25df24935ee5928e502c9f Mon Sep 17 00:00:00 2001 From: Yang Wen Date: Thu, 1 Sep 2016 22:20:05 +0800 Subject: [PATCH 08/24] add initilized data to seeds.rb --- db/seeds.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/db/seeds.rb b/db/seeds.rb index 1beea2a..f97c57b 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -5,3 +5,13 @@ # # movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }]) # Character.create(name: 'Luke', movie: movies.first) +Category.find_or_create_by(name: "Science") +Category.find_or_create_by(name: "Technology") +Category.find_or_create_by(name: "Engineering") +Category.find_or_create_by(name: "Art") +Category.find_or_create_by(name: "Math") +Article.find_or_create_by(title: "Hello World", + content: "test", + is_public: true, + category_id: 1, + user_id: 1) \ No newline at end of file From 1ba9ecdc2902c2d27c484a77b432457a3ec309ed Mon Sep 17 00:00:00 2001 From: Yang Wen Date: Thu, 1 Sep 2016 22:50:23 +0800 Subject: [PATCH 09/24] done articles CRUD features --- app/assets/stylesheets/application.scss | 6 +- app/controllers/articles_controller.rb | 59 ++++++ app/views/articles/_article_nav.html.erb | 12 ++ app/views/articles/_form.html.erb | 37 ++++ app/views/articles/edit.html.erb | 2 + app/views/articles/index.html.erb | 211 +++++-------------- app/views/articles/new.html.erb | 3 + app/views/articles/show.html.erb | 76 +++++++ app/views/shared/_flash.html.erb | 5 + db/migrate/20160901141023_create_articles.rb | 2 +- db/schema.rb | 4 +- 11 files changed, 256 insertions(+), 161 deletions(-) create mode 100644 app/views/articles/_article_nav.html.erb create mode 100644 app/views/articles/_form.html.erb create mode 100644 app/views/articles/edit.html.erb create mode 100644 app/views/articles/new.html.erb create mode 100644 app/views/articles/show.html.erb create mode 100644 app/views/shared/_flash.html.erb diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index 25c9603..b446d94 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -14,4 +14,8 @@ *= require_self */ @import "bootstrap-sprockets"; -@import "bootstrap"; \ No newline at end of file +@import "bootstrap"; + +body { + padding-top: 70px; +} \ No newline at end of file diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index a5d9666..cfd53a0 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -1,5 +1,64 @@ class ArticlesController < ApplicationController + before_action :load_article, only: [:show, :edit, :update, :destroy] + def index + @articles = Article.where(is_public: true).order('created_at DESC') + end + + def show + @categories = Category.all + end + + def edit end + + def update + build_article + respond_to do |format| + if @article.update(article_params) + format.html { redirect_to article_path, notice: "更新成功"} + else + format.html { render :edit } + end + end + end + + def new + @article = Article.new + end + + def create + build_article + respond_to do |format| + if @article.save + format.html { redirect_to @article, notice: "创建成功"} + else + format.html { render action: :index, error: "创建失败" } + end + end + end + + def destroy + @article.destroy + respond_to do |format| + format.html { redirect_to articles_url, notice: '删除成功' } + end + end + private + + # 根据url中的id查找对应article + def load_article + @article ||= Article.find(params[:id]) + end + + def build_article + @article ||= Article.new(article_params) + @article.attributes = article_params.merge(user_id: current_user.id) + end + + # strong parameters 检查 + def article_params + params.require(:article).permit(:title, :content, :is_public, :category_id, :user_id) + end end diff --git a/app/views/articles/_article_nav.html.erb b/app/views/articles/_article_nav.html.erb new file mode 100644 index 0000000..93c16ea --- /dev/null +++ b/app/views/articles/_article_nav.html.erb @@ -0,0 +1,12 @@ +
    +
    +

    Articles Home + All +

    + +
    +
    + \ No newline at end of file diff --git a/app/views/articles/_form.html.erb b/app/views/articles/_form.html.erb new file mode 100644 index 0000000..1b532fd --- /dev/null +++ b/app/views/articles/_form.html.erb @@ -0,0 +1,37 @@ +<%= form_for @article do |f| %> +
    +
    + +
    +
    + <%= f.text_area :content, id: "article_content", :type=> 'hidden' %> +
    +
    + <%= f.select :category_id, options_for_select(Category.all.map { |c| [c.name, c.id] } )%> +
    +
    + + <%= f.check_box :is_public, {}, 'true', 'false'%> +
    +
    + <%= f.submit '提交', class: 'submit' %> +
    +
    +<% end %> + + + \ No newline at end of file diff --git a/app/views/articles/edit.html.erb b/app/views/articles/edit.html.erb new file mode 100644 index 0000000..8237b23 --- /dev/null +++ b/app/views/articles/edit.html.erb @@ -0,0 +1,2 @@ +<%= render 'article_nav' %> +<%= render 'form' %> diff --git a/app/views/articles/index.html.erb b/app/views/articles/index.html.erb index 4925326..71464db 100644 --- a/app/views/articles/index.html.erb +++ b/app/views/articles/index.html.erb @@ -1,160 +1,57 @@ -
    - -
    -
    -

    Blog Home One - Subheading -

    - -
    -
    - - -
    - - -
    - - -

    - Blog Post Title -

    -

    - by Start Bootstrap -

    -

    Posted on August 28, 2013 at 10:00 PM

    -
    - - - -
    -

    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolore, veritatis, tempora, necessitatibus inventore nisi quam quia repellat ut tempore laborum possimus eum dicta id animi corrupti debitis ipsum officiis rerum.

    - Read More - -
    - - -

    - Blog Post Title -

    -

    - by Start Bootstrap -

    -

    Posted on August 28, 2013 at 10:45 PM

    -
    - - - -
    -

    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quibusdam, quasi, fugiat, asperiores harum voluptatum tenetur a possimus nesciunt quod accusamus saepe tempora ipsam distinctio minima dolorum perferendis labore impedit voluptates!

    - Read More - -
    - - -

    - Blog Post Title -

    -

    - by Start Bootstrap -

    -

    Posted on August 28, 2013 at 10:45 PM

    -
    - - - -
    -

    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Cupiditate, voluptates, voluptas dolore ipsam cumque quam veniam accusantium laudantium adipisci architecto itaque dicta aperiam maiores provident id incidunt autem. Magni, ratione.

    - Read More - -
    - - - - -
    - - -
    - - -
    -

    Blog Search

    -
    - - - - -
    - -
    - - -
    -

    Blog Categories

    - - -
    - - -
    -

    Side Widget Well

    -

    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Inventore, perspiciatis adipisci accusamus laudantium odit aliquam repellat tempore quos aspernatur vero.

    -
    - -
    + +
    +
    +
    +

    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Molestias, expedita, saepe, vero rerum deleniti beatae veniam harum neque nemo praesentium cum alias asperiores commodi.

    - - -
    - - -
    -
    -
    -

    Copyright © Your Website 2014

    -
    -
    -
    - -
    \ No newline at end of file +
    + <%= link_to("我要写文章", new_article_path , class: "btn btn-lg btn-default btn-block btn-success") %> +
    +
    +
    + +<%= render 'article_nav' %> + + +<% @articles.each do |article| %> +
    +
    +

    +

    +

    <%= article.created_at.strftime("%B, %d, %Y") %>

    +
    +
    + <%= link_to article do %> + <%= image_tag("article.png", class: "img-responsive img-hover") %> + <% end %> +
    +
    +

    + <%= link_to( article.title, article_path(article) ) %> +

    +

    + by <%= article.user.username %> +

    +

    <%= raw article.content %>

    + <%= link_to("Read More", article_path(article) , class: "btn btn-primary") %> +
    +
    + +
    +<% end %> + + + +
    + +
    + + +
    diff --git a/app/views/articles/new.html.erb b/app/views/articles/new.html.erb new file mode 100644 index 0000000..57495bc --- /dev/null +++ b/app/views/articles/new.html.erb @@ -0,0 +1,3 @@ +<%= render 'shared/flash' %> +<%= render 'article_nav' %> +<%= render 'form' %> diff --git a/app/views/articles/show.html.erb b/app/views/articles/show.html.erb new file mode 100644 index 0000000..8c8f027 --- /dev/null +++ b/app/views/articles/show.html.erb @@ -0,0 +1,76 @@ +<%= render 'article_nav' %> + + +
    + +
    + + +

    + Posted on <%= @article.created_at.strftime("%B, %d, %Y %I:%M %p") %> + <% if current_user %> + <%= link_to "编辑", edit_article_path(@article), class: "btn btn-primary pull-right" %> + <%= link_to "删除", article_path(@article), method: :delete, data: {confirm: "Are you sure?"}, class: "btn btn-danger pull-right"%> + <% end %> +

    + +
    + + + <%= raw @article.content %> +
    + + +
    + + +
    + + +
    +

    Blog Search

    +
    + + + + +
    + +
    + + +
    +

    Belong to Category: <%= @article.category.name %>

    +
    +
    +
      + <% @categories.each do |category| %> +
    • + <%= link_to( category.name) %> +
    • + <% end %> +
    +
    +
    + +
    + + + +
    + + + + + + + +
    + + +
    + +
    + + +
    \ No newline at end of file diff --git a/app/views/shared/_flash.html.erb b/app/views/shared/_flash.html.erb new file mode 100644 index 0000000..1074732 --- /dev/null +++ b/app/views/shared/_flash.html.erb @@ -0,0 +1,5 @@ +<% if flash[:notice] %> +

    <%= flash[:notice] %>

    +<% elsif flash[:error] %> +

    <%= flash[:error] %>

    +<% end %> \ No newline at end of file diff --git a/db/migrate/20160901141023_create_articles.rb b/db/migrate/20160901141023_create_articles.rb index f513e41..583474e 100644 --- a/db/migrate/20160901141023_create_articles.rb +++ b/db/migrate/20160901141023_create_articles.rb @@ -2,7 +2,7 @@ class CreateArticles < ActiveRecord::Migration[5.0] def change create_table :articles do |t| t.string :title - t.text :cotent + t.text :content t.boolean :is_public t.references :category, foreign_key: true t.belongs_to :user, foreign_key: true diff --git a/db/schema.rb b/db/schema.rb index 018798e..943623c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,11 +10,11 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160901141052) do +ActiveRecord::Schema.define(version: 20160901141339) do create_table "articles", force: :cascade do |t| t.string "title" - t.text "cotent" + t.text "content" t.boolean "is_public" t.integer "category_id" t.integer "user_id" From 6ea9d025d0183bc5df47b0e5f983ffc8b3dcac66 Mon Sep 17 00:00:00 2001 From: Yang Wen Date: Thu, 1 Sep 2016 22:51:00 +0800 Subject: [PATCH 10/24] add gem simditor --- Gemfile | 1 + Gemfile.lock | 2 ++ 2 files changed, 3 insertions(+) diff --git a/Gemfile b/Gemfile index 9868890..50b1bb6 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,6 @@ source 'https://rubygems.org' +gem 'simditor' gem 'devise' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 5.0.0', '>= 5.0.0.1' diff --git a/Gemfile.lock b/Gemfile.lock index 74676c8..4bf5067 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -135,6 +135,7 @@ GEM sprockets (>= 2.8, < 4.0) sprockets-rails (>= 2.0, < 4.0) tilt (>= 1.1, < 3) + simditor (2.3.6) spring (1.7.2) spring-watcher-listen (2.0.0) listen (>= 2.7, < 4.0) @@ -182,6 +183,7 @@ DEPENDENCIES puma (~> 3.0) rails (~> 5.0.0, >= 5.0.0.1) sass-rails (~> 5.0) + simditor spring spring-watcher-listen (~> 2.0.0) sqlite3 From ec9995c6da74117c4d0e42ed02bf47c41f47e78b Mon Sep 17 00:00:00 2001 From: Yang Wen Date: Thu, 1 Sep 2016 22:54:23 +0800 Subject: [PATCH 11/24] config simditor --- app/assets/javascripts/application.js | 1 + app/assets/stylesheets/application.scss | 1 + 2 files changed, 2 insertions(+) diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 759d92e..bfd194a 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -14,4 +14,5 @@ //= require jquery_ujs //= require turbolinks //= require bootstrap-sprockets +//= require simditor //= require_tree . diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index b446d94..9e0a9c9 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -10,6 +10,7 @@ * files in this directory. Styles in this file should be added after the last require_* statement. * It is generally better to create a new file per style scope. * + *= require simditor *= require_tree . *= require_self */ From 10f9ec9d7ca7520c21ec98ee8d2e1d54cacf4e2a Mon Sep 17 00:00:00 2001 From: Yang Wen Date: Thu, 1 Sep 2016 23:08:03 +0800 Subject: [PATCH 12/24] update README.md --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1de2d3e..a624a71 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ git push -u origin master ## 4. 用户注册&登录 使用[devise](https://github.com/plataformatec/devise)配置用户注册和登录功能。 看「GEtting started」完成登录功能: + 1. 在Gemfile文件中添加 `gem 'devise'`, 然后在命令行运行 `bundle install` 2. 在命令行运行 `rails g devise:install`; 3. 在文件config/environments/development.rb 中添加: @@ -81,9 +82,9 @@ git push -u origin master end end ``` -7. 在命令行输入 `rake routes` ,可以看到安装devise之后允许的路径,如下图: -6. 修改导航栏中的用户和注册路径,启动服务器,测试注册和登录功能 -7. git commit +8. 在命令行输入 `rake routes` ,可以看到安装devise之后允许的路径,如下图: +9. 修改导航栏中的用户和注册路径,启动服务器,测试注册和登录功能 +10. git commit ## 5. Articles MVC实践 1. 修改导航栏中Articles的路径 From b08ff8ee84ff3c3ff9d84cd9b8c9227e87ef0c95 Mon Sep 17 00:00:00 2001 From: Yang Wen Date: Thu, 1 Sep 2016 23:08:03 +0800 Subject: [PATCH 13/24] update README.md --- README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 1de2d3e..2edb6bd 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ git push -u origin master //= require bootstrap-sprockets ``` - `git commit -m 'add bootstrap'` - - `http://www.bootcss.com/p/layoutit/` 可视化布局首页 + - `http://www.bootcss.com/p/layoutit/` 可视化布局首页(我去这个地方下载了一个博客模板,省掉了自己写前端 https://startbootstrap.com/template-categories/all/ 。这个可能会增加复杂度,建议大家还是用模板工具,搭建简单的页面即可) 插入图片 - 下载代码,粘贴到 `app/views/visitors/index.html.erb` - 修改上一步的代码,并把引用的图片放到 `app/assetts/images`文件夹中 @@ -55,6 +55,7 @@ git push -u origin master ## 4. 用户注册&登录 使用[devise](https://github.com/plataformatec/devise)配置用户注册和登录功能。 看「GEtting started」完成登录功能: + 1. 在Gemfile文件中添加 `gem 'devise'`, 然后在命令行运行 `bundle install` 2. 在命令行运行 `rails g devise:install`; 3. 在文件config/environments/development.rb 中添加: @@ -81,9 +82,9 @@ git push -u origin master end end ``` -7. 在命令行输入 `rake routes` ,可以看到安装devise之后允许的路径,如下图: -6. 修改导航栏中的用户和注册路径,启动服务器,测试注册和登录功能 -7. git commit +8. 在命令行输入 `rake routes` ,可以看到安装devise之后允许的路径,如下图: +9. 修改导航栏中的用户和注册路径,启动服务器,测试注册和登录功能 +10. git commit ## 5. Articles MVC实践 1. 修改导航栏中Articles的路径 From ec8f79d955dd5c0568e19146c4f10564a7291d0f Mon Sep 17 00:00:00 2001 From: Yang Wen Date: Fri, 2 Sep 2016 11:32:01 +0800 Subject: [PATCH 14/24] add photos to README.md --- README.md | 60 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 2edb6bd..4a8e4bd 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ git push -u origin master ## 3. 制作首页(静态页面) 1. 在 config/routes.rb文件中添加 `root to: 'visitors#index'` 2. `rails s` 启动服务器,访问 `http://localhost:3000`。 遇到报错,提示 `没有初始化的常量 VisitorsController` (记得git commit) - 插入报错图片 + ![](http://ww3.sinaimg.cn/large/7853084cgw1f7f26szzcbj20sa0d6401.jpg) 3. 命令行运行 `rails g controller visitors index`; 再访问 `http://localhost:3000`, 无报错信息。 4. 使用[Bootstrap](http://getbootstrap.com)制作前端 - 观看[Gorails视频](https://gorails.com/episodes/styling-with-bootstrap-sass) (观看时把字幕打开), 以及参考 [bootstrap-sass](https://github.com/twbs/bootstrap-sass) @@ -34,7 +34,7 @@ git push -u origin master ``` - `git commit -m 'add bootstrap'` - `http://www.bootcss.com/p/layoutit/` 可视化布局首页(我去这个地方下载了一个博客模板,省掉了自己写前端 https://startbootstrap.com/template-categories/all/ 。这个可能会增加复杂度,建议大家还是用模板工具,搭建简单的页面即可) - 插入图片 + ![](http://ocuwjo7n4.bkt.clouddn.com/2016-09-02-WechatIMG19.jpeg) - 下载代码,粘贴到 `app/views/visitors/index.html.erb` - 修改上一步的代码,并把引用的图片放到 `app/assetts/images`文件夹中 - 创建共享文件夹 `mkdir app/views/shared` @@ -70,7 +70,9 @@ git push -u origin master
    ``` 然后在运行 `rails db:migrate` 迁移数据库 -7. permit additional parameters (the lazy way™), you can do so using a simple before filter in your ApplicationController: +7. 在文件app/application_controller.rb增添如下代码,目的是让devise的用户注册功能能写入数据库字段 `:username` + > permit additional parameters (the lazy way™), you can do so using a simple before filter in your ApplicationController: + ``` class ApplicationController < ActionController::Base before_action :configure_permitted_parameters, if: :devise_controller? @@ -83,10 +85,22 @@ git push -u origin master end ``` 8. 在命令行输入 `rake routes` ,可以看到安装devise之后允许的路径,如下图: -9. 修改导航栏中的用户和注册路径,启动服务器,测试注册和登录功能 +9. 修改导航栏中的用户和注册路径,启动服务器,测试注册和登录功能. 请参考我的源码。 10. git commit ## 5. Articles MVC实践 +![](http://ocuwjo7n4.bkt.clouddn.com/2016-09-02-railsmvc3.png) +> 网站 = 算法 + 数据(数据是核心) +> MVC就是算法,我们写算法的步骤就如上图。 +> 1. 从步骤1开始,浏览器输入网址。 +> 2. 浏览器到路由(config/routes.rb) +> 3. 路由解析浏览器传过来的网址,传到相对应controller里面的action; +> 4. action里面的方法决定从Model里面拿什么数据回来; +> 5. 在Model层面写好模型的数据类型以及相关模型的关联关系; +> 6. 把需要的数据给controller +> 7. controller拿到数据以后,保存在实例变量里面,传给view层; +> view层渲染上html/css/javascripts,再返回给controller; +> 8. controller发送给浏览器 1. 修改导航栏中Articles的路径 ```
  • @@ -94,10 +108,12 @@ git push -u origin master
  • ``` 2. 刷新浏览器,点击导航栏Articles,跳转到 `http://localhost:3000/articles`,报错,如下图: +![](http://ocuwjo7n4.bkt.clouddn.com/2016-09-02-BlogDemo4.jpeg) 3. `routes.rb` 中添加 `resources :articles` 4. 启动服务器 `rails s`, 访问 `http://localhost:3000/articles`, **找不到路径**错误已解决。但有一个新的 **找不到controller** 报错,报错如下图 - 插入图 + ![](http://ocuwjo7n4.bkt.clouddn.com/2016-09-02-BlogDemo2.jpeg) 5. 在命令行输入 `rails g controller articles`, 刷新浏览器。报错,提示controller里面没有 ‘index’ action,如下图: +![](http://ocuwjo7n4.bkt.clouddn.com/2016-09-02-BlogDemo6.jpeg) 6. 在 app/controllers/articles_controller.rb 文件夹中添加index方法 ``` class ArticlesController < ApplicationController @@ -107,14 +123,14 @@ git push -u origin master ``` 再刷新浏览器。报错,提示:没有 index template -7. 在命令行中输入 `touch app/views/articles/index.html.erb` +7. 新建index页面:在命令行中输入 `touch app/views/articles/index.html.erb` 8. 再使用[可视化bootstrap工具](http://www.bootcss.com/p/layoutit/)制作文章页面,把代码复制过来。如下图效果: 9. 上面的还都是静态页面,没有model。现在我们开始**建立artile的model及其与其他模型的关联**: - - 在命令行输入 `rails g model Article title:string content:text is_public:boolean category:references user:belongs_to` - - `rails model Category name:string` : Article模型和Category模型是关联模型 - - `rails db:migrate` - - 在 app/models/category.rb 添加内容如下 + - 创建Article Model:在命令行输入 `rails g model Article title:string content:text is_public:boolean category:references user:belongs_to` + - 创建Category Model:`rails model Category name:string` : Article模型和Category模型是关联模型 + - 数据迁移:`rails db:migrate` + - 模型关系:在 app/models/category.rb 添加内容如下 ``` class Category < ApplicationRecord has_many :articles @@ -130,7 +146,7 @@ git push -u origin master has_many :articles end ``` - - 在db/seeds.rb 中添加如下内容 + - 初始化数据:在db/seeds.rb 中添加如下内容 ``` Category.find_or_create_by(name: "Science") Category.find_or_create_by(name: "Technology") @@ -139,14 +155,14 @@ git push -u origin master Category.find_or_create_by(name: "Math") ``` 在命令行运行 `rails db:seed` 初始化categories表格的数据 - - 完善articles_controller.rb中的index方法:从数据库中读入数据(C-M的数据交流) + - 让controller从model里面拿数据:完善articles_controller.rb中的index方法:从数据库中读入数据(C-M的数据交流) ``` def index @articles = Article.where(is_public: true).order('created_at DESC') end ``` - 完善 app/views/articles/index.html.erb -10. 做完article的CRUD功能, 从controller到view的顺序更新。分别是:`show -> edit -> update -> new -> create -> destroy` +10. 按照index的步骤做完article的CRUD功能, 从controller到view的顺序更新。分别是:`show -> edit -> update -> new -> create -> destroy` - show - 分享插件: http://www.jiathis.com - edit @@ -156,11 +172,11 @@ git push -u origin master - http://www.printshit.me/blog/2016/05/30/how-to-use-simditor-in-rails/ - https://ruby-china.org/topics/28467 -6. Comment 实践(Polymorphic Association) - 1. Creating a Single Comment Model - `rails g model comment content:text commentable_id:integer commentable_type:string` - 2. 修改 migration, 使用多态技巧polymorphic如下: - ``` +## 6. Comment 实践(Polymorphic Association) +1. Creating a Single Comment Model +`rails g model comment content:text commentable_id:integer commentable_type:string` +2. 修改 migration, 使用多态技巧polymorphic如下: + ``` class CreateComments < ActiveRecord::Migration[5.0] def change create_table :comments do |t| @@ -175,7 +191,7 @@ git push -u origin master ``` 然后 `rails db:migrate` - http://railscasts.com/episodes/154-polymorphic-association-revised?view=asciicast - https://rubyplus.com/articles/3901-Polymorphic-Association-in-Rails-5 - https://gorails.com/episodes/comments-with-polymorphic-associations - https://github.com/gorails-screencasts/gorails-episode-36 +http://railscasts.com/episodes/154-polymorphic-association-revised?view=asciicast +https://rubyplus.com/articles/3901-Polymorphic-Association-in-Rails-5 +https://gorails.com/episodes/comments-with-polymorphic-associations +https://github.com/gorails-screencasts/gorails-episode-36 From 07a590ac15c3e2dd312052bafae488fd394f583a Mon Sep 17 00:00:00 2001 From: Yang Wen Date: Fri, 2 Sep 2016 11:48:04 +0800 Subject: [PATCH 15/24] fix README.md layout bug --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4a8e4bd..230cac9 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,7 @@ git push -u origin master ![](http://ocuwjo7n4.bkt.clouddn.com/2016-09-02-railsmvc3.png) > 网站 = 算法 + 数据(数据是核心) > MVC就是算法,我们写算法的步骤就如上图。 +> > 1. 从步骤1开始,浏览器输入网址。 > 2. 浏览器到路由(config/routes.rb) > 3. 路由解析浏览器传过来的网址,传到相对应controller里面的action; @@ -99,8 +100,9 @@ git push -u origin master > 5. 在Model层面写好模型的数据类型以及相关模型的关联关系; > 6. 把需要的数据给controller > 7. controller拿到数据以后,保存在实例变量里面,传给view层; -> view层渲染上html/css/javascripts,再返回给controller; +> view层渲染上html/css/javascripts,再返回给controller; > 8. controller发送给浏览器 + 1. 修改导航栏中Articles的路径 ```
  • From 870eec330abaf4e9abb927405fad5582aa9ac9d2 Mon Sep 17 00:00:00 2001 From: Yang Wen Date: Thu, 8 Sep 2016 20:36:53 +0800 Subject: [PATCH 16/24] update README.md: comment --- README.md | 89 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 74 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 230cac9..1ab5831 100644 --- a/README.md +++ b/README.md @@ -175,23 +175,82 @@ git push -u origin master - https://ruby-china.org/topics/28467 ## 6. Comment 实践(Polymorphic Association) -1. Creating a Single Comment Model -`rails g model comment content:text commentable_id:integer commentable_type:string` -2. 修改 migration, 使用多态技巧polymorphic如下: - ``` - class CreateComments < ActiveRecord::Migration[5.0] - def change - create_table :comments do |t| - t.text :content - t.belongs_to :commentable, polymorphic: true - - t.timestamps - end - add_index :comments, [:commentable_id, :commentable_type] - end +#### 6.1 Creating a Single Comment Model +1. 创建模型:命令行中运行 `rails g model Comment content:text commentable:references{polymorphic} user:belongs_to` +2. 添加相关模型关联关系: + ``` + # app/model/user.rb + class User < ApplicationRecord + # Include default devise modules. Others available are: + # :confirmable, :lockable, :timeoutable and :omniauthable + devise :database_authenticatable, :registerable, + :recoverable, :rememberable, :trackable, :validatable + has_many :articles + has_many :comments, as: :commentable + end + ``` + + ``` + class Article < ApplicationRecord + belongs_to :category + belongs_to :user + has_many :comments, as: :commentable + end + ``` + +3. 然后 `rails db:migrate` +#### 6.2 Using a Polymorphic Association in our Application +1. 修改路由 + ``` + # config/routes.rb + resources :articles do + resources :comments, only: [:index, :new, :create] + end + ``` +2. 创建controller `rails g controller comments index new create` +3. 修改index 方法,从Comment模型读取数据 + ``` + def index + @commentable = Article.find(params[:article_id]) + @comments = @commentable.comments + end + ``` +4. 修改 app/views/comments/index.html.erb 内容如下: + ``` + # app/views/comments/index.html.erb +

    Comments

    + <%= render 'comments' %> + ``` + ``` + # app/views/comments/_comment.html.erb + + <% @comments.each do |comment| %> +
    +
    +

    <%= comment.user.username %> + comment on <%= comment.created_at.strftime("%B, %d, %Y %I:%M %p") %> +

    + <%= simple_format comment.content %> +
    +
    + <% end %> + ``` +4. 在 `app/views/articles/show.html.erb` 文章内容下添加: + ``` + <%= render "comments/comments" %> + ``` + 启动服务器, 访问`http://localhost:3000/articles/1`,报错:提示each的对象 @comments 不能为空。因此我们要在article controller的show action里面添加存取出@comments的方法 +5. 更改 `app/controllers/articles_controller.rb` show de action + ``` + def show + @categories = Category.all + # prepare comment instance variables for comment view partials + @commentable = @article + @comments = @commentable.comments.order('created_at DESC') + @comment = Comment.new end ``` - 然后 `rails db:migrate` +6. http://railscasts.com/episodes/154-polymorphic-association-revised?view=asciicast https://rubyplus.com/articles/3901-Polymorphic-Association-in-Rails-5 From 18478b97ef6384957aa39e633ac643096e9d3cbc Mon Sep 17 00:00:00 2001 From: Yang Wen Date: Thu, 8 Sep 2016 20:49:27 +0800 Subject: [PATCH 17/24] create model comments polymorphic:true --- README.md | 8 ++++---- app/models/comment.rb | 4 ++++ db/migrate/20160908124243_create_comments.rb | 11 +++++++++++ test/fixtures/comments.yml | 13 +++++++++++++ test/models/comment_test.rb | 7 +++++++ 5 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 app/models/comment.rb create mode 100644 db/migrate/20160908124243_create_comments.rb create mode 100644 test/fixtures/comments.yml create mode 100644 test/models/comment_test.rb diff --git a/README.md b/README.md index 1ab5831..0aec183 100644 --- a/README.md +++ b/README.md @@ -252,7 +252,7 @@ git push -u origin master ``` 6. -http://railscasts.com/episodes/154-polymorphic-association-revised?view=asciicast -https://rubyplus.com/articles/3901-Polymorphic-Association-in-Rails-5 -https://gorails.com/episodes/comments-with-polymorphic-associations -https://github.com/gorails-screencasts/gorails-episode-36 +- http://railscasts.com/episodes/154-polymorphic-association-revised?view=asciicast +- https://rubyplus.com/articles/3901-Polymorphic-Association-in-Rails-5 +- https://gorails.com/episodes/comments-with-polymorphic-associations +- https://github.com/gorails-screencasts/gorails-episode-36 diff --git a/app/models/comment.rb b/app/models/comment.rb new file mode 100644 index 0000000..d39d62d --- /dev/null +++ b/app/models/comment.rb @@ -0,0 +1,4 @@ +class Comment < ApplicationRecord + belongs_to :commentable, polymorphic: true + belongs_to :user +end diff --git a/db/migrate/20160908124243_create_comments.rb b/db/migrate/20160908124243_create_comments.rb new file mode 100644 index 0000000..f822195 --- /dev/null +++ b/db/migrate/20160908124243_create_comments.rb @@ -0,0 +1,11 @@ +class CreateComments < ActiveRecord::Migration[5.0] + def change + create_table :comments do |t| + t.text :content + t.references :commentable, polymorphic: true + t.belongs_to :user, foreign_key: true + + t.timestamps + end + end +end diff --git a/test/fixtures/comments.yml b/test/fixtures/comments.yml new file mode 100644 index 0000000..e5a5864 --- /dev/null +++ b/test/fixtures/comments.yml @@ -0,0 +1,13 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + content: MyText + commentable: one + commentable_type: Commentable + user: one + +two: + content: MyText + commentable: two + commentable_type: Commentable + user: two diff --git a/test/models/comment_test.rb b/test/models/comment_test.rb new file mode 100644 index 0000000..b6d6131 --- /dev/null +++ b/test/models/comment_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class CommentTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end From 265193a8146e9d026713e19e6ceb0fed8e79ba33 Mon Sep 17 00:00:00 2001 From: Yang Wen Date: Thu, 8 Sep 2016 20:50:06 +0800 Subject: [PATCH 18/24] add association of comments --- app/models/article.rb | 1 + app/models/user.rb | 1 + 2 files changed, 2 insertions(+) diff --git a/app/models/article.rb b/app/models/article.rb index 9c5f63c..3ec8bb5 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -1,4 +1,5 @@ class Article < ApplicationRecord belongs_to :category belongs_to :user + has_many :comments, as: :commentable end diff --git a/app/models/user.rb b/app/models/user.rb index c3c343b..0d5ad01 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -4,4 +4,5 @@ class User < ApplicationRecord devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable has_many :articles + has_many :comments, as: :commentable end From 0fb5d1c7a38e16d54a9c8b9d82c15d0b62a8d69e Mon Sep 17 00:00:00 2001 From: Yang Wen Date: Thu, 8 Sep 2016 20:51:53 +0800 Subject: [PATCH 19/24] rails db:migrate --- db/schema.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/db/schema.rb b/db/schema.rb index 943623c..ae78080 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160901141339) do +ActiveRecord::Schema.define(version: 20160908124243) do create_table "articles", force: :cascade do |t| t.string "title" @@ -30,6 +30,17 @@ t.datetime "updated_at", null: false end + create_table "comments", force: :cascade do |t| + t.text "content" + t.string "commentable_type" + t.integer "commentable_id" + t.integer "user_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["commentable_type", "commentable_id"], name: "index_comments_on_commentable_type_and_commentable_id" + t.index ["user_id"], name: "index_comments_on_user_id" + end + create_table "users", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false From bab045f090b3aac804fe52b92cc184195a751aa2 Mon Sep 17 00:00:00 2001 From: Yang Wen Date: Thu, 8 Sep 2016 20:53:42 +0800 Subject: [PATCH 20/24] add comments resources to routes.rb --- config/routes.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/config/routes.rb b/config/routes.rb index 64c45b6..9f874e4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,5 +2,7 @@ devise_for :users # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html root to: 'visitors#index' - resources :articles + resources :articles do + resources :comments, only: [:index, :new, :create] + end end From 8f7532f9c7cb9514f2cc47ade226f50a507deb65 Mon Sep 17 00:00:00 2001 From: Yang Wen Date: Thu, 8 Sep 2016 21:05:39 +0800 Subject: [PATCH 21/24] create controller comments --- app/assets/javascripts/comments.coffee | 3 +++ app/assets/stylesheets/comments.scss | 3 +++ app/controllers/comments_controller.rb | 9 +++++++++ app/helpers/comments_helper.rb | 2 ++ app/views/comments/_comments.html.erb | 12 ++++++++++++ app/views/comments/index.html.erb | 3 +++ app/views/comments/new.html.erb | 2 ++ config/routes.rb | 4 ++++ test/controllers/comments_controller_test.rb | 14 ++++++++++++++ 9 files changed, 52 insertions(+) create mode 100644 app/assets/javascripts/comments.coffee create mode 100644 app/assets/stylesheets/comments.scss create mode 100644 app/controllers/comments_controller.rb create mode 100644 app/helpers/comments_helper.rb create mode 100644 app/views/comments/_comments.html.erb create mode 100644 app/views/comments/index.html.erb create mode 100644 app/views/comments/new.html.erb create mode 100644 test/controllers/comments_controller_test.rb diff --git a/app/assets/javascripts/comments.coffee b/app/assets/javascripts/comments.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/comments.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/comments.scss b/app/assets/stylesheets/comments.scss new file mode 100644 index 0000000..3722c12 --- /dev/null +++ b/app/assets/stylesheets/comments.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the comments controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb new file mode 100644 index 0000000..5c684ab --- /dev/null +++ b/app/controllers/comments_controller.rb @@ -0,0 +1,9 @@ +class CommentsController < ApplicationController +def index + @commentable = Article.find(params[:article_id]) + @comments = @commentable.comments +end + + def new + end +end diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb new file mode 100644 index 0000000..0ec9ca5 --- /dev/null +++ b/app/helpers/comments_helper.rb @@ -0,0 +1,2 @@ +module CommentsHelper +end diff --git a/app/views/comments/_comments.html.erb b/app/views/comments/_comments.html.erb new file mode 100644 index 0000000..4ee6411 --- /dev/null +++ b/app/views/comments/_comments.html.erb @@ -0,0 +1,12 @@ +# app/views/comments/_comment.html.erb + +<% @comments.each do |comment| %> +
    +
    +

    <%= comment.user.username %> + comment on <%= comment.created_at.strftime("%B, %d, %Y %I:%M %p") %> +

    + <%= simple_format comment.content %> +
    +
    +<% end %> \ No newline at end of file diff --git a/app/views/comments/index.html.erb b/app/views/comments/index.html.erb new file mode 100644 index 0000000..dcca44f --- /dev/null +++ b/app/views/comments/index.html.erb @@ -0,0 +1,3 @@ +

    Comments

    +<%= render 'comments' %> + diff --git a/app/views/comments/new.html.erb b/app/views/comments/new.html.erb new file mode 100644 index 0000000..0b8b6ce --- /dev/null +++ b/app/views/comments/new.html.erb @@ -0,0 +1,2 @@ +

    Comments#new

    +

    Find me in app/views/comments/new.html.erb

    diff --git a/config/routes.rb b/config/routes.rb index 9f874e4..a9c0f52 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,8 @@ Rails.application.routes.draw do + get 'comments/index' + + get 'comments/new' + devise_for :users # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html root to: 'visitors#index' diff --git a/test/controllers/comments_controller_test.rb b/test/controllers/comments_controller_test.rb new file mode 100644 index 0000000..1e08c8d --- /dev/null +++ b/test/controllers/comments_controller_test.rb @@ -0,0 +1,14 @@ +require 'test_helper' + +class CommentsControllerTest < ActionDispatch::IntegrationTest + test "should get index" do + get comments_index_url + assert_response :success + end + + test "should get new" do + get comments_new_url + assert_response :success + end + +end From df55cf0f3231924b02d80bbd766c846881f14e41 Mon Sep 17 00:00:00 2001 From: Yang Wen Date: Thu, 8 Sep 2016 21:10:45 +0800 Subject: [PATCH 22/24] done comments index feature --- README.md | 4 ++-- app/controllers/articles_controller.rb | 4 ++++ app/views/articles/show.html.erb | 1 + 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0aec183..25bf38a 100644 --- a/README.md +++ b/README.md @@ -207,7 +207,7 @@ git push -u origin master resources :comments, only: [:index, :new, :create] end ``` -2. 创建controller `rails g controller comments index new create` +2. 创建controller `rails g controller comments index new` 3. 修改index 方法,从Comment模型读取数据 ``` def index @@ -215,7 +215,7 @@ git push -u origin master @comments = @commentable.comments end ``` -4. 修改 app/views/comments/index.html.erb 内容如下: +4. 修改 app/views/comments/index.html.erb 内容如下: ``` # app/views/comments/index.html.erb

    Comments

    diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index cfd53a0..9ae151e 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -7,6 +7,10 @@ def index def show @categories = Category.all + # prepare comment instance variables for comment view partials + @commentable = @article + @comments = @commentable.comments.order('created_at DESC') + @comment = Comment.new end def edit diff --git a/app/views/articles/show.html.erb b/app/views/articles/show.html.erb index 8c8f027..b96c342 100644 --- a/app/views/articles/show.html.erb +++ b/app/views/articles/show.html.erb @@ -20,6 +20,7 @@ <%= raw @article.content %>
    + <%= render "comments/comments" %> From 565d5ee684f2f7bfd658022d3c189306de7c3cc4 Mon Sep 17 00:00:00 2001 From: Yang Wen Date: Thu, 8 Sep 2016 21:26:02 +0800 Subject: [PATCH 23/24] add comments/new views --- app/views/comments/_form.html.erb | 23 +++++++++++++++++++++++ app/views/comments/new.html.erb | 3 +-- 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 app/views/comments/_form.html.erb diff --git a/app/views/comments/_form.html.erb b/app/views/comments/_form.html.erb new file mode 100644 index 0000000..aa62689 --- /dev/null +++ b/app/views/comments/_form.html.erb @@ -0,0 +1,23 @@ +
    +

    Leave a Comment

    + + <%= form_for [@commentable, @comment] do |f| %> + <% if @comment.errors.any? %> +
    +

    Please correct the following errors.

    +
      + <% @comment.errors.full_messages.each do |msg| %> +
    • <%= msg %>
    • + <% end %> +
    +
    + <% end %> + +
    + <%= f.text_area :content, rows: 5, class: 'form-control' %> +
    +
    + <%= f.submit %> +
    + <% end %> +
    \ No newline at end of file diff --git a/app/views/comments/new.html.erb b/app/views/comments/new.html.erb index 0b8b6ce..96486a3 100644 --- a/app/views/comments/new.html.erb +++ b/app/views/comments/new.html.erb @@ -1,2 +1 @@ -

    Comments#new

    -

    Find me in app/views/comments/new.html.erb

    +<%= render 'form' %> \ No newline at end of file From 47d372e8c185420f612caad7ca83dc9bb364611b Mon Sep 17 00:00:00 2001 From: Yang Wen Date: Thu, 8 Sep 2016 21:26:35 +0800 Subject: [PATCH 24/24] add comment form in article show page --- app/controllers/comments_controller.rb | 31 ++++++++++++++++++++++---- app/views/articles/show.html.erb | 3 ++- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 5c684ab..5bb67be 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -1,9 +1,32 @@ class CommentsController < ApplicationController -def index - @commentable = Article.find(params[:article_id]) - @comments = @commentable.comments -end + before_action :authenticate_user!, except: [:index] + before_action :load_commentable + + def index + @comments = @commentable.comments + end def new + @comment = @commentable.comments.new + end + + def create + @comment = @commentable.comments.new(comment_params) + @comment.user = current_user + if @comment.save + redirect_to @commentable, notice: "评论成功" + else + render :new + end + end + + private + def load_commentable + resource, id = request.path.split('/')[1,2] + @commentable = resource.singularize.classify.constantize.find(id) + end + + def comment_params + params.require(:comment).permit(:content, :user_id) end end diff --git a/app/views/articles/show.html.erb b/app/views/articles/show.html.erb index b96c342..d140a09 100644 --- a/app/views/articles/show.html.erb +++ b/app/views/articles/show.html.erb @@ -19,7 +19,8 @@ <%= raw @article.content %>
    - + + <%= render "comments/form" %> <%= render "comments/comments" %>