Add Topic/Comment validate

This commit is contained in:
Rei
2014-02-15 15:20:54 +08:00
parent 928811352f
commit 23e91b9cb5
8 changed files with 18 additions and 19 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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 %>

View File

@@ -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

View File

@@ -0,0 +1,3 @@
<% if @topic.errors.empty? %>
Turbolinks.visit('<%= topic_path(@topic) %>');
<% end %>

View File

@@ -0,0 +1,3 @@
<% if @topic.errors.empty? %>
Turbolinks.visit('<%= topic_path(@topic) %>');
<% end %>

View File

@@ -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