diff --git a/app/controllers/forums_controller.rb b/app/controllers/forums_controller.rb index be1a331e0..991af0154 100644 --- a/app/controllers/forums_controller.rb +++ b/app/controllers/forums_controller.rb @@ -11,6 +11,42 @@ class ForumsController < ApplicationController include SortHelper PageLimit = 20 + + def create_memo + @memo = Memo.new(params[:memo]) + @memo.forum_id = @forum.id + @memo.author_id = User.current.id + + @memo.save_attachments(params[:attachments] || (params[:memo] && params[:memo][:uploads])) + + respond_to do |format| + if @memo.save + format.html { redirect_to (forum_memo_path(@forum, (@memo.parent_id.nil? ? @memo : @memo.parent_id))), notice: "#{l :label_memo_create_succ}" } + format.json { render json: @memo, status: :created, location: @memo } + else + sort_init 'updated_at', 'desc' + sort_update 'created_at' => "#{Memo.table_name}.created_at", + 'replies' => "#{Memo.table_name}.replies_count", + 'updated_at' => "COALESCE (last_replies_memos.created_at, #{Memo.table_name}.created_at)" + + @topic_count = @forum.topics.count + @topic_pages = Paginator.new @topic_count, per_page_option, params['page'] + @memos = @forum.topics. + reorder("#{Memo.table_name}.sticky DESC"). + includes(:last_reply). + limit(@topic_pages.per_page). + offset(@topic_pages.offset). + order(sort_clause). + preload(:author, {:last_reply => :author}). + all + + flash.now[:error] = "#{l :label_memo_create_fail}: #{@memo.errors.full_messages[0]}" + # back_error_page = @memo.parent_id.nil? ? forum_path(@forum) : forum_memo_path(@forum, @memo.parent_id) + format.html { render action: :show, layout: 'base_forums' }#, error: "#{l :label_memo_create_fail}: #{@memo.errors.full_messages[0]}" } + format.json { render json: @memo.errors, status: :unprocessable_entity } + end + end + end def index @offset, @limit = api_offset_and_limit({:limit => 10}) diff --git a/app/controllers/memos_controller.rb b/app/controllers/memos_controller.rb index a21086da3..f6ad48801 100644 --- a/app/controllers/memos_controller.rb +++ b/app/controllers/memos_controller.rb @@ -47,7 +47,30 @@ class MemosController < ApplicationController else flash[:error] = "#{l :label_memo_create_fail}: #{@memo.errors.full_messages[0]}" # back_error_page = @memo.parent_id.nil? ? forum_path(@forum) : forum_memo_path(@forum, @memo.parent_id) - format.html { redirect_to back_memo_or_forum_url}#, error: "#{l :label_memo_create_fail}: #{@memo.errors.full_messages[0]}" } + pre_count = REPLIES_PER_PAGE + + @memo_new = @memo.dup + @memo = @memo.root # 取出楼主,防止输入帖子id让回复作为主贴显示 + @memo.update_column(:viewed_count, (@memo.viewed_count.to_i + 1)) + + page = params[:page] + if params[:r] && page.nil? + offset = @memo.children.where("#{Memo.table_name}.id < ?", params[:r].to_i).count + page = 1 + offset / pre_count + else + + end + @reply_count = @memo.children.count + @reply_pages = Paginator.new @reply_count, pre_count, page + @replies = @memo.children. + includes(:author, :attachments). + reorder("#{Memo.table_name}.created_at ASC"). + limit(@reply_pages.per_page). + offset(@reply_pages.offset). + all + + format.html { render action: :show }#, error: "#{l :label_memo_create_fail}: #{@memo.errors.full_messages[0]}" } + # format.html { redirect_to back_memo_or_forum_url}#, error: "#{l :label_memo_create_fail}: #{@memo.errors.full_messages[0]}" } format.json { render json: @memo.errors, status: :unprocessable_entity } end end @@ -76,7 +99,7 @@ class MemosController < ApplicationController offset(@reply_pages.offset). all - @mome_new = Memo.new + @memo_new = Memo.new # @memo = Memo.find_by_id(params[:id]) diff --git a/app/models/memo.rb b/app/models/memo.rb index 2e4ddb810..a0581d945 100644 --- a/app/models/memo.rb +++ b/app/models/memo.rb @@ -4,9 +4,10 @@ class Memo < ActiveRecord::Base belongs_to :author, :class_name => "User", :foreign_key => 'author_id' validates_presence_of :author_id, :forum_id, :subject - #validates :content, presence: true + # 若是主题帖,则内容可以是空 + validates :content, presence: true, if: Proc.new{|o| !o.parent_id.nil? } validates_length_of :subject, maximum: 50 - #validates_length_of :content, maximum: 3072 + validates_length_of :content, maximum: 3072 validate :cannot_reply_to_locked_topic, :on => :create acts_as_tree :counter_cache => :replies_count, :order => "#{Memo.table_name}.created_at ASC" diff --git a/app/views/forums/show.html.erb b/app/views/forums/show.html.erb index f3f0524ae..677416a6d 100644 --- a/app/views/forums/show.html.erb +++ b/app/views/forums/show.html.erb @@ -2,7 +2,7 @@