+ <% 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
- 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/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/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/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/application.scss b/app/assets/stylesheets/application.scss
index 25c9603..9e0a9c9 100644
--- a/app/assets/stylesheets/application.scss
+++ b/app/assets/stylesheets/application.scss
@@ -10,8 +10,13 @@
* 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
*/
@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/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/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/articles_controller.rb b/app/controllers/articles_controller.rb
new file mode 100644
index 0000000..9ae151e
--- /dev/null
+++ b/app/controllers/articles_controller.rb
@@ -0,0 +1,68 @@
+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
+ # prepare comment instance variables for comment view partials
+ @commentable = @article
+ @comments = @commentable.comments.order('created_at DESC')
+ @comment = Comment.new
+ 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/controllers/comments_controller.rb b/app/controllers/comments_controller.rb
new file mode 100644
index 0000000..5bb67be
--- /dev/null
+++ b/app/controllers/comments_controller.rb
@@ -0,0 +1,32 @@
+class CommentsController < ApplicationController
+ 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/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/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/models/article.rb b/app/models/article.rb
new file mode 100644
index 0000000..3ec8bb5
--- /dev/null
+++ b/app/models/article.rb
@@ -0,0 +1,5 @@
+class Article < ApplicationRecord
+ belongs_to :category
+ belongs_to :user
+ has_many :comments, as: :commentable
+end
diff --git a/app/models/category.rb b/app/models/category.rb
new file mode 100644
index 0000000..419c22f
--- /dev/null
+++ b/app/models/category.rb
@@ -0,0 +1,3 @@
+class Category < ApplicationRecord
+ has_many :articles
+end
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/app/models/user.rb b/app/models/user.rb
index b2091f9..0d5ad01 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -3,4 +3,6 @@ class User < ApplicationRecord
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
+ has_many :articles
+ has_many :comments, as: :commentable
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| %>
+
+<% 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
new file mode 100644
index 0000000..71464db
--- /dev/null
+++ b/app/views/articles/index.html.erb
@@ -0,0 +1,57 @@
+
+
+
+
+
+
+
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Molestias, expedita, saepe, vero rerum deleniti beatae veniam harum neque nemo praesentium cum alias asperiores commodi.
+<% end %>
\ No newline at end of file
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| %>
+
\ 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..96486a3
--- /dev/null
+++ b/app/views/comments/new.html.erb
@@ -0,0 +1 @@
+<%= render 'form' %>
\ 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/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? %>
diff --git a/config/routes.rb b/config/routes.rb
index faaec11..a9c0f52 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -1,5 +1,12 @@
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'
+ resources :articles do
+ resources :comments, only: [:index, :new, :create]
+ end
end
diff --git a/db/migrate/20160901141023_create_articles.rb b/db/migrate/20160901141023_create_articles.rb
new file mode 100644
index 0000000..583474e
--- /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 :content
+ 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/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/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/db/schema.rb b/db/schema.rb
index e5c8535..ae78080 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,36 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20160901134415) do
+ActiveRecord::Schema.define(version: 20160908124243) do
+
+ create_table "articles", force: :cascade do |t|
+ t.string "title"
+ t.text "content"
+ 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 "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
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
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
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
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/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/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/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
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
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