基於Ruby on Rails的論壇實現(新手指南)

用Rails實現一個簡單的論壇系統,大致的架構爲:站點擁有多個Forum,每個Forum有多個Topic,而每個Topic又有多個Reply。原文鏈接:[url]http://railsonedge.blogspot.com/2008/02/rails-forum-tutorial-for-beginners-part.html[/url]。

我下載的是目前最新的Rails版本2.1,數據庫使用的是Sql Server 2000,應用程序所在目錄爲F:\Ruby\railsdoc\myforum。首先快速地依照以下步驟創建Forum類:

[list=1][*]執行[color=red]rails myforum[/color]生成完整的目錄結構;
[*]執行[color=red]ruby script/generate scaffold Forum name:string description:text[/color]創建相關的數據庫移植文件及model、controller、views文件;
[*]修改database.yml文件中的相關數據庫配置信息;
[*]在Sql Server中新建名爲myforum的數據庫,並執行[color=red]rake db:migrate[/color]創建forums表;
[*]修改environment.rb文件使頁面能正常顯示中文。[/list]
這樣Forum頁面就完成了,啓動服務器後在瀏覽器鍵入http://127.0.0.1:3000/forums便能進行CRUD操作了。

刪除/public/index.html,修改/config/routes.rb,使得http://localhost:3000直接指向Forum列表:
[quote]map.root :controller => 'forums', :action => 'index'[/quote]

分別執行以下命令創建Topic和Reply的相關文件及數據庫表:
[quote]ruby script/generate scaffold Topic forum:references user:references subject:string body:text

ruby script/generate scaffold Reply topic:references user:references subject:string body:text

rake db:migrate[/quote]

表Forum
[img]/upload/attachment/43459/16483034-0716-32ef-962a-0a966ac167bd.jpg[/img]

表Topic
[img]/upload/attachment/43461/0914d1eb-130c-3588-a162-2390068d7c60.jpg[/img]

表Reply
[img]/upload/attachment/43463/e220aa77-6bfe-376f-a043-04094f002db9.jpg[/img]

修改/config/routes.rb
……
map.resources :forums, :has_many => :topics # Add
map.resources :topics, :has_many => :replies # Add
map.resources :replies
……


修改/app/models/forum.rb
class Forum < ActiveRecord::Base
has_many :topics # Add
has_many :replies, :through => :topics # Add
end


修改/app/models/topic.rb
class Topic < ActiveRecord::Base
belongs_to :forum # Add
belongs_to :user # Add
has_many :replies # Add
end


修改/app/models/reply.rb
class Reply < ActiveRecord::Base
belongs_to :topic # Add
belongs_to :user # Add
end


修改/app/controllers/topics_controller.rb
class TopicsController < ApplicationController
before_filter :load_forum # Add

def load_forum # Add
@forum = Forum.find(params[:forum_id]) # Add
end # Add

# GET /topics
# GET /topics.xml
def index
@topics = @forum.topics # Edit: @topics = Topic.find(:all)

respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @topics }
end
end

# GET /topics/1
# GET /topics/1.xml
def show
@topic = @forum.topics.find(params[:id]) # Edit: @topic = Topic.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @topic }
end
end

# GET /topics/new
# GET /topics/new.xml
def new
@topic = @forum.topics.build # Edit: @topic = Topic.new

respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @topic }
end
end

# GET /topics/1/edit
def edit
@topic = @forum.topics.find(params[:id]) # Edit: @topic = Topic.find(params[:id])
end

# POST /topics
# POST /topics.xml
def create
@topic = @forum.topics.build(params[:topic]) # Edit: @topic = Topic.new(params[:topic])

respond_to do |format|
if @topic.save
flash[:notice] = 'Topic was successfully created.'
format.html { redirect_to(@forum) } # Edit: format.html { redirect_to(@topic) }
format.xml { render :xml => @topic, :status => :created, :location => @topic }
else
format.html { render :action => "new" }
format.xml { render :xml => @topic.errors, :status => :unprocessable_entity }
end
end
end

# PUT /topics/1
# PUT /topics/1.xml
def update
@topic = @forum.topics.find(params[:id]) # Edit: @topic = Topic.find(params[:id])

respond_to do |format|
if @topic.update_attributes(params[:topic])
flash[:notice] = 'Topic was successfully updated.'
format.html { redirect_to(@forum) } # Edit: format.html { redirect_to(@topic) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @topic.errors, :status => :unprocessable_entity }
end
end
end

# DELETE /topics/1
# DELETE /topics/1.xml
def destroy
@topic = @forum.topics.find(params[:id]) # Edit: @topic = Topic.find(params[:id])
@topic.destroy

respond_to do |format|
format.html { redirect_to(forum_topics_url(@forum)) } # Edit: format.html { redirect_to(topics_url) }
format.xml { head :ok }
end
end
end


以上修改基於兩個目的:一、使得對於Topic的操作能關聯到正確的外鍵(forum_id),二、在對Topic執行CUD操作後將能返回至相應的Forum頁面,而不是Topic頁面。

將/app/views/forums/show.html.erb
<p>
<b>Name:</b>
<%=h @forum.name %>
</p>

<p>
<b>Description:</b>
<%=h @forum.description %>
</p>


<%= link_to 'Edit', edit_forum_path(@forum) %> |
<%= link_to 'Back', forums_path %>

修改爲
<p>
<b>Forum:</b>
<%=h @forum.name %> - <%=h @forum.description %>
</p>

<% unless @forum.topics.empty? %>
<h2>Topics</h2>
<% @forum.topics.each do |topic| %>
<b><%= link_to topic.subject, [@forum, topic] %></b><br />
<% end %>
<% end %>

<h2>New Post</h2>
<%= render :partial => @topic = Topic.new, :locals => { :button_name => 'Create' } %>

<%= link_to 'Edit', edit_forum_path(@forum) %> |
<%= link_to 'Back', forums_path %>


可以看到/app/views/topics/new.html.erb和/app/views/topics/edit.html.erb相當類似,我們創建一個新的文件/app/views/topics/_topic.html.erb
<% form_for([@forum, @topic]) do |f| %>
<p>
<b>Subject</b><br />
<%= f.text_field :subject %>
</p>

<p>
<b>Body</b><br />
<%= f.text_area :body %>
</p>

<p>
<%= f.submit button_name %>
</p>
<% end %>


將/app/views/topics/new.html.erb
<h1>New topic</h1>

<% form_for(@topic) do |f| %>
<%= f.error_messages %>

<p>
<%= f.label :forum %><br />
<%= f.text_field :forum %>
</p>
<p>
<%= f.label :user %><br />
<%= f.text_field :user %>
</p>
<p>
<%= f.label :subject %><br />
<%= f.text_field :subject %>
</p>
<p>
<%= f.label :body %><br />
<%= f.text_area :body %>
</p>
<p>
<%= f.submit "Create" %>
</p>
<% end %>

<%= link_to 'Back', topics_path %>

修改爲
<h1>New topic</h1>

<%= error_messages_for :topic %>

<%= render :partial => @topic, :locals => { :button_name => "Create" } %>

<%= link_to 'Back', topics_path %>


將/app/views/topics/edit.html.erb
<h1>Editing topic</h1>

<% form_for(@topic) do |f| %>
<%= f.error_messages %>

<p>
<%= f.label :forum %><br />
<%= f.text_field :forum %>
</p>
<p>
<%= f.label :user %><br />
<%= f.text_field :user %>
</p>
<p>
<%= f.label :subject %><br />
<%= f.text_field :subject %>
</p>
<p>
<%= f.label :body %><br />
<%= f.text_area :body %>
</p>
<p>
<%= f.submit "Update" %>
</p>
<% end %>

<%= link_to 'Show', @topic %> |
<%= link_to 'Back', topics_path %>

修改爲
<h1>Editing topic</h1>

<%= error_messages_for :topic %>

<%= render :partial => @topic, :locals => { :button_name => "Submit" } %>

<%= link_to 'Show', [@forum, @topic] %> |
<%= link_to 'Back', topics_path %>


目前部分已完成Forum和Topic的關聯,以下是操作頁面截圖:
[b]Forum List[/b]
[img]/upload/attachment/43477/76cf0cd4-1727-351b-9095-ff692e40b1ab.jpg[/img]

[b]Topic List[/b]
[img]/upload/attachment/43479/26f8421f-21ef-393f-963e-02f6e5e84a59.jpg[/img]

[b]Topic Details[/b]
[img]/upload/attachment/43481/72e37cfc-5355-3ec1-97f4-d874890336d1.jpg[/img]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章