2013-09-28 108 views
0

我正在使用Ruby on Rails創建一個論壇(我很新),並設法讓自己完全陷入困境。在類別中放置論壇(Rails 4)

**版Ruby on Rails的4.0 **

一個論壇軟件可以有很多類別,這些類別中可以有多個論壇。

主要頁面將類似於此:

1類

  • 論壇1
  • 論壇2

類別2

  • 論壇3
  • 論壇4
  • 論壇5

當你創建一個論壇,你應該有一個下拉菜單,讓您選擇您希望把它放在哪一類。

起初,我創建兩個不同的腳手架 - 一個用於分類和一個用於論壇。我用一個外鍵來連接這兩個。我不知道這是否是最好的方法,但我根本無法讓他們互動。我最終搞砸了我的代碼,所以我很少爲它展示。

我嘗試使用Adding Sub-categories in Rails4categories and sub-categories model rails解決方案,但都最終導致錯誤。

這是我的一些代碼。這並不多,但也許你可以告訴我哪裏可以開始。如果有更好的方法(不使用兩張表),請告訴我。我很樂意聽到這樣做的最佳方式不使用寶石

警告:我的代碼是一團糟。

遷移

class AddForeignToForums < ActiveRecord::Migration 
     def change 
     add_column :forums, :category_id, :integer 
     end 
    end 

論壇控制器(我知道我失去了一些東西,讓我連接到範疇,我只是不知道是什麼)

類ForumsController < ApplicationController before_action:set_forum,only:[:show,:edit,:update,:destroy]

# GET function. view/forums/index.html.erb 
    def index 
    @forums = Forum.all 
    end 

    # GET /forums/1. view/forums/show.html.erb 
    def show 
    @forum = Forum.find(params[:id]) 
    end 

    # GET /forums/new. view/forums/new.html.erb 
    # Be able to list all the Categories. 
    def new 
    @forum = Forum.new 
    @categories = Category.all 

    end 

    # GET /forums/1/edit 
    # Be able to list all the categories. 
    def edit 
    @forum = Forum.find(params[:id]) 
    @categories = Category.all 
    end 

    # POST /forums 
    # Allows the creation of a new forum 
    # Lindsey note: how to save category_idea. Assign to category. 
    def create 
    @forum = Forum.new(forum_params) 

    respond_to do |format| 
     if @forum.save 
     @forum = Forum.new(:name => params[:forum][:name], 
     :category_id => params[:forum][:category_id]) 

     format.html { redirect_to @forum, notice: 'Forum was successfully created.' } 
     format.json { render action: 'show', status: :created, location: @forum } 
     else 
     format.html { render action: 'new' } 
     format.json { render json: @forum.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /forums/1 
    # Allows the update of forums. 
    def update 
    respond_to do |format| 
     if @forum.update(forum_params) 
     format.html { redirect_to @forum, notice: 'Forum was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render action: 'edit' } 
     format.json { render json: @forum.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /forums/1 
    def destroy 
    @forum.destroy 
    respond_to do |format| 
     format.html { redirect_to forums_url } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_forum 
     @forum = Forum.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def forum_params 
     params.require(:forum).permit(:name, :description, :category_id) 
    end 
end 

論壇模式

class Forum < ActiveRecord::Base 
    belongs_to :category 
end 

分類模式

class Category < ActiveRecord::Base 
    has_many :forums, :dependent => :destroy, 
end 

類別Index.html.erb

<tbody> 
    <% @categories.each do |category| %> 
     <tr> 
     <td><%= link_to category.name, category %></td> 
     <td><%= link_to ' (Edit', edit_category_path(category) %></td> 
     <td><%= link_to '| Destroy)', category, method: :delete, data: { confirm: 'Are you sure?' } %></td> 
     </tr> 

     <% category.forums.each do |forum| %> 
     <tr> 
      <td><li><%= link_to forum.name, forum %></li></td> 
     </tr> 
     <% end %> 
    <% end %> 
    </tbody> 

對於嗯_form.html.erb

<%= form_for(@forum) do |f| %> 
<div class="field"> 
    <%= f.label :name %><br> 
    <%= f.text_field :name %> 
</div> 
<div class="field"> 
    <%= f.label :description %><br> 
    <%= f.text_area :description %> 
</div> 

<%= f.label :category_id %><br /> 
<%= f.select :category_id, Category.all.map{|c| [c.name, c.id]} %> 

<div class="actions"> 
    <%= f.submit %> 
</div> 

回答

2

你可能想對論壇的一個表中,類型的表,連接表包括forum_id和CATEGORY_ID - 命名此forum_categories

class Forum < ActiveRecord::Base 

    has_many :forum_categories 
    has_many :categories, :through => :forum_categories 

end 

並且,對於類別,您將執行相反操作

class Categories < ActiveRecord::Base 

    has_many :forum_categories 
    has_many :forums, :through => :forum_categories 

end 

要在視圖中添加類別,可以使用複選框或一個多選框。此輸入的名稱將會是

f.check_box 'category_ids[]' 

f.select 'category_ids[]' 

這將提交陣列形式設置了一個param,讓你用一個簡單的

forum.create(params[:forum]) 
更新forum.category_ids

在你看來,而不是@forums,你會列出category.forums

<% category.forums.each do |forum| %> 
    <%= forum.name %> 
    <% end %> 

希望這會讓你開始。

編輯

對於論壇上的單一類別,你做得很好。就在幾個小的變化:

class Category < ActiveRecord::Base 

    # belongs_to :category - this can be removed 
    has_many :forums # Do you want to delete the forums if the category is removed? You don't need the classname option. 

end 

在下拉 - 你會做這樣的事情...

f.select :category_id, Category.all.map{|c| [c.name, c.id]} 
+0

我還能想連接表,如果關係不是很多,TO-許多?我的數據庫的這一部分具有一對多的關係,因爲論壇只能分配給一個類別。 – Lindsiria

+0

對不起 - 我誤解了。你用category_id很好。我補充了一些答案。 – Swards