我正在使用Rails创建一个论坛(我在这方面非常新),并且已经完全陷入了困境。
** Rails 4.0版**
一个论坛软件可以有很多类别,在这些类别中你可以有多个论坛。
主页看起来类似于以下内容:
类别1
第2类
当您创建一个论坛,您应该有一个下拉菜单,允许您选择您希望放置在哪个类别。
起初,我创建了两种不同的支架--一种用于类别,另一种用于论坛。我用外键把两者连接起来。我不知道这是否是最好的方法,但我根本无法让他们互动。最后,我把我的代码搞砸了,我没什么可展示的。
我尝试使用Adding Sub-categories in Rails4和categories and sub-categories model rails作为解决方案,但最后都导致了错误。
这是我的一些代码。不多,但也许你可以告诉我从哪里开始。如果有更好的方法(不使用两个表),请告诉我。我很想听听在不使用gems的情况下做这个的最佳方法。
警告:我的代码乱七八糟。
迁移
class AddForeignToForums < ActiveRecord::Migration
def change
add_column :forums, :category_id, :integer
end
end论坛控制器(我知道我遗漏了一些东西,可以让我连接到类别,我只是不知道是什么)
类ForumsController < ApplicationController before_action :set_forum,仅:显示、:编辑、: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>发布于 2013-09-28 05:42:14
您可能需要一个用于论坛的表、一个用于类别的表和一个包含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])在您的视图中,您将列出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]}https://stackoverflow.com/questions/19063485
复制相似问题