我正在使用Ruby on Rails創建一個論壇(我很新),並設法讓自己完全陷入困境。在類別中放置論壇(Rails 4)
**版Ruby on Rails的4.0 **
一個論壇軟件可以有很多類別,這些類別中可以有多個論壇。
主要頁面將類似於此:
1類
- 論壇1
- 論壇2
類別2
- 論壇3
- 論壇4
- 論壇5
當你創建一個論壇,你應該有一個下拉菜單,讓您選擇您希望把它放在哪一類。
起初,我創建兩個不同的腳手架 - 一個用於分類和一個用於論壇。我用一個外鍵來連接這兩個。我不知道這是否是最好的方法,但我根本無法讓他們互動。我最終搞砸了我的代碼,所以我很少爲它展示。
我嘗試使用Adding Sub-categories in Rails4和categories 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>
我還能想連接表,如果關係不是很多,TO-許多?我的數據庫的這一部分具有一對多的關係,因爲論壇只能分配給一個類別。 – Lindsiria
對不起 - 我誤解了。你用category_id很好。我補充了一些答案。 – Swards