diff --git a/Gemfile b/Gemfile index ba5db1e..3d2db6c 100644 --- a/Gemfile +++ b/Gemfile @@ -23,6 +23,7 @@ gem "mini_magick" gem 'carrierwave-mongoid' gem 'html_truncator' gem 'nokogiri' +gem 'angularjs-rails' group :development do gem 'quiet_assets' diff --git a/app/assets/javascripts/angularjs.js.coffee b/app/assets/javascripts/angularjs.js.coffee new file mode 100644 index 0000000..24d09f6 --- /dev/null +++ b/app/assets/javascripts/angularjs.js.coffee @@ -0,0 +1,10 @@ +#= require angular +#= require angular-resource +#= require_self +#= require_tree ./angularjs + +@app = angular.module('app', []) + +@app.config(["$httpProvider", (provider) -> + provider.defaults.headers.common['X-CSRF-Token'] = $('meta[name=csrf-token]').attr('content') +]) diff --git a/app/assets/javascripts/angularjs/comments.js.coffee b/app/assets/javascripts/angularjs/comments.js.coffee new file mode 100644 index 0000000..776cbab --- /dev/null +++ b/app/assets/javascripts/angularjs/comments.js.coffee @@ -0,0 +1,25 @@ +@app.controller 'CommentsController', ($scope, $http, $location, $timeout)-> + url = $location.absUrl() + "/comments" + + $http.get(url).success (data)-> + console.log data + $scope.comments = data + + $scope.publish_success = null + + $scope.submit = -> + comment = { content: $scope.content, name: $scope.name, email: $scope.email } + $http.post(url, comment) + .success (res)-> + if res.success + $scope.publish_success = true + $scope.content = '' + $scope.comments.unshift(res.data) + else + $scope.publish_success = false + $timeout -> + $scope.publish_success = null + , 3*1000 + + .error (data)-> + alert(data) diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 94ab678..e2bb1c0 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -1,6 +1,7 @@ //= require jquery //= require jquery_ujs //= require foundation +//= require angularjs //= require 'jquery.html5-fileupload' //= require_tree . diff --git a/app/assets/javascripts/blogs.js.coffee b/app/assets/javascripts/blogs.js.coffee deleted file mode 100644 index 9512762..0000000 --- a/app/assets/javascripts/blogs.js.coffee +++ /dev/null @@ -1,10 +0,0 @@ -# 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://jashkenas.github.com/coffee-script/ - -$(document).ready -> - $('.pagination').click -> - l = window.location.pathname - page = $(this).attr('data-page') - window.location= l + "?page=#{page}" - false diff --git a/app/assets/javascripts/home.js.coffee b/app/assets/javascripts/home.js.coffee deleted file mode 100644 index 7615679..0000000 --- a/app/assets/javascripts/home.js.coffee +++ /dev/null @@ -1,3 +0,0 @@ -# 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://jashkenas.github.com/coffee-script/ diff --git a/app/assets/javascripts/photos.js.coffee b/app/assets/javascripts/photos.js.coffee deleted file mode 100644 index 7615679..0000000 --- a/app/assets/javascripts/photos.js.coffee +++ /dev/null @@ -1,3 +0,0 @@ -# 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://jashkenas.github.com/coffee-script/ diff --git a/app/assets/stylesheets/comments.css.scss b/app/assets/stylesheets/comments.css.scss index d109988..0940f93 100644 --- a/app/assets/stylesheets/comments.css.scss +++ b/app/assets/stylesheets/comments.css.scss @@ -48,3 +48,11 @@ border-bottom: 1px dashed #8a8a8a; } } + +.comment-success { + color: #66FAB5; +} + +.comment-fail { + color: #FF7A7A; +} diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb new file mode 100644 index 0000000..1ff0363 --- /dev/null +++ b/app/controllers/comments_controller.rb @@ -0,0 +1,37 @@ +class CommentsController < ApplicationController + layout false + helper_method :format_time + def index + @post = Post.find( params[:blog_id] ) + res = @post.comments.desc(:created_at).collect { |comment| build_json(comment) } + render :json => res + end + + def create + @post = Post.find( params[:blog_id] ) + comment = Comment.new(comment_params) + comment.post = @post + if comment.save + render :json=> { success: true, data: build_json(comment) } + else + render :json=> { success: false } + end + end + + private + def comment_params + params.permit(:content, :name, :email) + end + + def format_time(time) + time.strftime("%Y-%m-%d %H:%M") + end + + def build_json(comment) + { + content: comment.content, + name: comment.name, + 'created_at' => format_time(comment.created_at) + } + end +end diff --git a/app/helpers/blogs_helper.rb b/app/helpers/blogs_helper.rb deleted file mode 100644 index 2dff537..0000000 --- a/app/helpers/blogs_helper.rb +++ /dev/null @@ -1,5 +0,0 @@ -module BlogsHelper - def format_time(time) - time.strftime("%Y-%m-%d %H:%M") - end -end diff --git a/app/helpers/home_helper.rb b/app/helpers/home_helper.rb deleted file mode 100644 index 23de56a..0000000 --- a/app/helpers/home_helper.rb +++ /dev/null @@ -1,2 +0,0 @@ -module HomeHelper -end diff --git a/app/helpers/photos_helper.rb b/app/helpers/photos_helper.rb deleted file mode 100644 index 0a10d47..0000000 --- a/app/helpers/photos_helper.rb +++ /dev/null @@ -1,2 +0,0 @@ -module PhotosHelper -end diff --git a/app/models/comment.rb b/app/models/comment.rb index eb912b6..726081f 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -11,4 +11,5 @@ class Comment validates :name, presence: true validates :email, confirmation: true,:format => /@/ validates :content, presence: true + validates_presence_of :post_id end diff --git a/app/views/blogs/_comment.html.slim b/app/views/blogs/_comment.html.slim index 3a155bc..bc06d4b 100644 --- a/app/views/blogs/_comment.html.slim +++ b/app/views/blogs/_comment.html.slim @@ -1,18 +1,21 @@ -.row - .small-12.large-12.columns - = text_area_tag(:content, nil, placeholder: '发表你的看法') -.row - .small-12.large-6.columns - = text_field_tag(:name, nil, placeholder: '你的名字') - = text_field_tag(:email, nil, placeholder: '你的邮箱') -button 发表 -.comment-diag - p.name - | WinDy • - span.created-at 2014-2-12 13:00 - p.comment-content 写的好!! - - p.name - | WinDy • - span.created-at 2014-2-12 13:00 - p.comment-content 写的好!! +.row ng-app="app" ng-controller="CommentsController" + .small-12.large-8.columns + form + .row + .small-12.large-12.columns + = text_area_tag(:content, nil, placeholder: '发表你的看法', 'ng-model'=> 'content') + .row + .small-12.large-6.columns + = text_field_tag(:name, nil, placeholder: '你的名字', 'ng-model'=> 'name') + = text_field_tag(:email, nil, placeholder: '你的邮箱', 'ng-model'=> 'email', 'ng-pattern'=>"/@/", 'ng-required'=>"true") + button ng-click="submit()" ng-disabled="form.$invalid" 发表 + p.comment-success ng-show="publish_success" 发布成功 + p.comment-fail ng-show="publish_success == false" 发布失败 + .comment-diag + .comment-wrapper ng-repeat=" comment in comments " + p.name + |{{ comment.name + " • " }} + span.created-at + |{{ comment.created_at }} + p.comment-content + |{{ comment.content }} diff --git a/app/views/blogs/show.html.slim b/app/views/blogs/show.html.slim index 4271134..8c7f0c2 100644 --- a/app/views/blogs/show.html.slim +++ b/app/views/blogs/show.html.slim @@ -2,9 +2,7 @@ .small-12.large-8.columns = render partial: "post", :locals=> { :post=> @post } .comment-field - .row - .small-12.large-8.columns - = render partial: 'comment', locals: { comments: @comments } + = render partial: 'comment', locals: { comments: @comments, post: @post } .row .small-12.large-8.columns - if @prev diff --git a/app/views/comments/create.html.slim b/app/views/comments/create.html.slim new file mode 100644 index 0000000..26aeccd --- /dev/null +++ b/app/views/comments/create.html.slim @@ -0,0 +1,2 @@ +h1 Comments#create +p Find me in app/views/comments/create.html.slim diff --git a/app/views/comments/index.html.slim b/app/views/comments/index.html.slim new file mode 100644 index 0000000..70499d4 --- /dev/null +++ b/app/views/comments/index.html.slim @@ -0,0 +1,2 @@ +h1 Comments#index +p Find me in app/views/comments/index.html.slim diff --git a/config/routes.rb b/config/routes.rb index bc26009..c213f76 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,8 +4,10 @@ WBlog::Application.routes.draw do collection do get :rss end + resources :comments, only: [:index, :create] end + resources :archives # photos resources :photos, :only=>[:create] diff --git a/spec/controllers/comments_controller_spec.rb b/spec/controllers/comments_controller_spec.rb new file mode 100644 index 0000000..562bf1a --- /dev/null +++ b/spec/controllers/comments_controller_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe CommentsController do + +end diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb index d632891..b504a0a 100644 --- a/spec/models/comment_spec.rb +++ b/spec/models/comment_spec.rb @@ -2,13 +2,13 @@ require 'spec_helper' describe Comment do it 'test comment' do - a = Comment.new(name: '1',content: '2432423',email: '22@.com') + a = Comment.new(name: '1',content: '2432423',email: '22@.com', post_id: 1) a.save.should == true - b = Comment.new(content: '2432ddd423',email: '22@.com') + b = Comment.new(content: '2432ddd423',email: '22@.com', post_id: 1) b.save.should == false - b = Comment.new(name: '2', email: '22@.com') + b = Comment.new(name: '2', email: '22@.com', post_id: 1) b.save.should == false - b = Comment.new(name: '2', content: '2432ddd423',email: '22.com') + b = Comment.new(name: '2', content: '2432ddd423',email: '22.com', post_id: 1) b.save.should == false end end