在类别中放置论坛(Rails 4)

时间:2013-09-28 04:58:55

标签: ruby-on-rails ruby migration ruby-on-rails-4

我正在使用Ruby on Rails创建一个论坛(我对此非常陌生)并且设法让自己陷入困境。

** 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&lt; 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>

1 个答案:

答案 0 :(得分: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[]'

这将以数组格式提交一个参数,允许您使用简单的

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

在您的视图中,您将列出category.forums

,而不是@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]}
相关问题