我正在使用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