Class: Homeland::TopicsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/homeland/topics_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#current_user, #homeland_require_user

Instance Method Details

#createObject

POST /topics POST /topics.xml



83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/controllers/homeland/topics_controller.rb', line 83

def create
  pt = params[:topic]
  @topic = Topic.new(pt)
  @topic.user_id = current_user.id
  @topic.node_id = params[:node] || pt[:node_id]

  if @topic.save
    redirect_to(topic_path(@topic.id), :notice => '帖子创建成功.')
  else
    render :action => "new"
  end
end

#destroyObject

DELETE /topics/1 DELETE /topics/1.xml



114
115
116
117
118
# File 'app/controllers/homeland/topics_controller.rb', line 114

def destroy
  @topic = current_user.topics.find(params[:id])
  @topic.destroy
  redirect_to(topics_path, :notice => '帖子删除成功.')
end

#editObject

GET /topics/1/edit



76
77
78
79
# File 'app/controllers/homeland/topics_controller.rb', line 76

def edit
  @topic = current_user.topics.find(params[:id])
  @node = @topic.node
end

#feedObject



24
25
26
27
28
# File 'app/controllers/homeland/topics_controller.rb', line 24

def feed
  @topics = Topic.recent.limit(20)
  response.headers['Content-Type'] = 'application/rss+xml'
  render :layout => false
end

#indexObject

GET /topics GET /topics.xml



18
19
20
21
22
# File 'app/controllers/homeland/topics_controller.rb', line 18

def index
  @topics = Topic.last_actived.limit(10).includes(:user)
  @sections = Section.all

end

#newObject

GET /topics/new GET /topics/new.xml



51
52
53
54
55
56
57
58
59
60
61
# File 'app/controllers/homeland/topics_controller.rb', line 51

def new
  @topic = Topic.new
  if !params[:node].blank?
    @topic.node_id = params[:node]
    @node = Node.where(:_id => params[:node]).first
    if @node.blank?
      redirect_to "/404"
    end
  end
  @topic.id = nil
end

#nodeObject



30
31
32
33
34
35
# File 'app/controllers/homeland/topics_controller.rb', line 30

def node
  @node = Node.find(params[:id])
  @topics = @node.topics.last_actived.includes(:user).paginate(:page => params[:page],:per_page => 50)

  render :action => "index"
end

#recentObject



37
38
39
40
41
# File 'app/controllers/homeland/topics_controller.rb', line 37

def recent
  @topics = Topic.recent.limit(50).includes(:user)

  render :action => "index"
end

#replyObject



63
64
65
66
67
68
69
70
71
72
73
# File 'app/controllers/homeland/topics_controller.rb', line 63

def reply
  @topic = Topic.find(params[:id])
  @reply = @topic.replies.build(params[:reply])
  @reply.user_id = current_user.id
  if @reply.save
    flash[:notice] = "回复成功。"
  else
    flash[:notice] = @reply.errors.full_messages.join("<br />")
  end
  redirect_to topic_path(params[:id],:anchor => "reply")
end

#showObject



43
44
45
46
47
# File 'app/controllers/homeland/topics_controller.rb', line 43

def show
  @topic = Topic.find(params[:id])
  @node = @topic.node
  @replies = @topic.replies.all.includes(:user)
end

#updateObject

PUT /topics/1 PUT /topics/1.xml



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/controllers/homeland/topics_controller.rb', line 98

def update
  @topic = current_user.topics.find(params[:id])
  pt = params[:topic]
  @topic.node_id = pt[:node_id]
  @topic.title = pt[:title]
  @topic.body = pt[:body]

  if @topic.save
    redirect_to(topic_path(@topic.id), :notice => '帖子修改成功.')
  else
    render :action => "edit"
  end
end