mirror of
https://github.com/zhigang1992/tsemple.git
synced 2026-01-12 17:52:57 +08:00
Add Topic/Comment validate
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 %>
|
||||
|
||||
@@ -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
|
||||
|
||||
3
app/views/topics/create.js.erb
Normal file
3
app/views/topics/create.js.erb
Normal file
@@ -0,0 +1,3 @@
|
||||
<% if @topic.errors.empty? %>
|
||||
Turbolinks.visit('<%= topic_path(@topic) %>');
|
||||
<% end %>
|
||||
3
app/views/topics/update.js.erb
Normal file
3
app/views/topics/update.js.erb
Normal file
@@ -0,0 +1,3 @@
|
||||
<% if @topic.errors.empty? %>
|
||||
Turbolinks.visit('<%= topic_path(@topic) %>');
|
||||
<% end %>
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user