From 69d3ab5bef40715b6e51ad64a9047a8ccd038460 Mon Sep 17 00:00:00 2001 From: yanxd Date: Sat, 23 Nov 2013 10:47:28 +0800 Subject: [PATCH] add memo controller --- app/controllers/memos_controller.rb | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/app/controllers/memos_controller.rb b/app/controllers/memos_controller.rb index 7ba5aec43..101ae69e2 100644 --- a/app/controllers/memos_controller.rb +++ b/app/controllers/memos_controller.rb @@ -1,5 +1,5 @@ class MemosController < ApplicationController - + layout "base_memos" def new @memo = Memo.new @@ -12,6 +12,7 @@ class MemosController < ApplicationController def show @memo = Memo.find(params[:id]) @replies = @memo.replies + @mome_new = Memo.new respond_to do |format| format.html # show.html.erb @@ -22,11 +23,24 @@ class MemosController < ApplicationController def create @memo = Memo.new(params[:memo]) @memo.author_id = User.current.id + @back_memo_id = @memo.id + + if @memo.parent_id + @back_memo_id ||= @memo.parent_id + @parent_memo = Memo.find_by_id(@memo.parent_id) + @parent_memo.replies_count += 1 + end respond_to do |format| if @memo.save - format.html { redirect_to @memo, notice: 'Memo was successfully created.' } - format.json { render json: @memo, status: :created, location: @memo } + @parent_memo.last_reply_id = @memo.id if @parent_memo + if @parent_memo.save + format.html { redirect_to forum_memo_path(@memo.forum_id, @back_memo_id), notice: 'Memo was successfully created.' } + format.json { render json: @memo, status: :created, location: @memo } + else + format.html { redirect_to forum_memo_path(@memo.forum_id, @back_memo_id) } + format.json { render json: @memo.errors, status: :unprocessable_entity } + end else format.html { render action: "new" } format.json { render json: @memo.errors, status: :unprocessable_entity }