From 23e91b9cb58a08bf3350bef1a659712fb6cf765e Mon Sep 17 00:00:00 2001 From: Rei Date: Sat, 15 Feb 2014 15:20:54 +0800 Subject: [PATCH] Add Topic/Comment validate --- app/controllers/topics_controller.rb | 14 ++------------ app/models/comment.rb | 1 + app/models/topic.rb | 2 ++ app/views/comments/create.js.erb | 6 ++++-- app/views/topics/_form.html.slim | 2 +- app/views/topics/create.js.erb | 3 +++ app/views/topics/update.js.erb | 3 +++ test/controllers/topics_controller_test.rb | 6 ++---- 8 files changed, 18 insertions(+), 19 deletions(-) create mode 100644 app/views/topics/create.js.erb create mode 100644 app/views/topics/update.js.erb diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb index 6f88506..b6b3ada 100644 --- a/app/controllers/topics_controller.rb +++ b/app/controllers/topics_controller.rb @@ -43,24 +43,14 @@ class TopicsController < ApplicationController end def create - @topic = current_user.topics.new topic_params - - if @topic.save - redirect_to @topic - else - render :new - end + @topic = current_user.topics.create topic_params end def edit end def update - if @topic.update_attributes topic_params - redirect_to @topic - else - render :edit - end + @topic.update_attributes topic_params end def trash diff --git a/app/models/comment.rb b/app/models/comment.rb index 9f4023c..5795dcd 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -8,6 +8,7 @@ class Comment < ActiveRecord::Base validates :commentable_type, inclusion: { in: %w(Topic) } validates :commentable, :user, presence: true + validates :body, presence: true after_create :create_mention_notification, :create_comment_notification diff --git a/app/models/topic.rb b/app/models/topic.rb index f751df3..445a7cf 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -7,6 +7,8 @@ class Topic < ActiveRecord::Base belongs_to :category, counter_cache: true has_many :comments, as: 'commentable' + validates :title, :body, presence: true + after_create :owner_subscribe def calculate_hot diff --git a/app/views/comments/create.js.erb b/app/views/comments/create.js.erb index 8f55aa1..a4c3827 100644 --- a/app/views/comments/create.js.erb +++ b/app/views/comments/create.js.erb @@ -1,2 +1,4 @@ -$('#comments-for-<%= @comment.commentable_type.downcase %>-<%= @comment.commentable_id %>').append('<%= j render @comment %>'); -$('#new_comment textarea').val(''); +<% if @comment.errors.empty? %> + $('#comments-for-<%= @comment.commentable_type.downcase %>-<%= @comment.commentable_id %>').append('<%= j render @comment %>'); + $('#new_comment textarea').val(''); +<% end %> diff --git a/app/views/topics/_form.html.slim b/app/views/topics/_form.html.slim index fdab876..641fc61 100644 --- a/app/views/topics/_form.html.slim +++ b/app/views/topics/_form.html.slim @@ -1,4 +1,4 @@ -= form_for @topic, html: { class: 'form-topic' } do |f| += form_for @topic, remote: true, html: { class: 'form-topic' } do |f| .row .col-md-9 .form-group diff --git a/app/views/topics/create.js.erb b/app/views/topics/create.js.erb new file mode 100644 index 0000000..46c6842 --- /dev/null +++ b/app/views/topics/create.js.erb @@ -0,0 +1,3 @@ +<% if @topic.errors.empty? %> + Turbolinks.visit('<%= topic_path(@topic) %>'); +<% end %> diff --git a/app/views/topics/update.js.erb b/app/views/topics/update.js.erb new file mode 100644 index 0000000..46c6842 --- /dev/null +++ b/app/views/topics/update.js.erb @@ -0,0 +1,3 @@ +<% if @topic.errors.empty? %> + Turbolinks.visit('<%= topic_path(@topic) %>'); +<% end %> diff --git a/test/controllers/topics_controller_test.rb b/test/controllers/topics_controller_test.rb index 87ba9a8..2572889 100644 --- a/test/controllers/topics_controller_test.rb +++ b/test/controllers/topics_controller_test.rb @@ -29,11 +29,10 @@ class TopicsControllerTest < ActionController::TestCase test "should create topic" do login_as create(:user) assert_difference "Topic.count" do - post :create, topic: attributes_for(:topic) + xhr :post, :create, topic: attributes_for(:topic) end topic = Topic.last assert_equal topic.user, topic.user - assert_redirected_to topic end test "should edit topic" do @@ -46,11 +45,10 @@ class TopicsControllerTest < ActionController::TestCase test "should update topic" do topic = create(:topic) login_as topic.user - patch :update, id: topic, topic: { title: 'change', body: 'change' } + xhr :patch, :update, id: topic, topic: { title: 'change', body: 'change' } topic.reload assert_equal 'change', topic.title assert_equal 'change', topic.body - assert_redirected_to topic end test "should not create topic for locked user" do