0

私は、Ruby on Rails を使用してフォーラムを作成している最中です (これはかなり新しいものです)。

**バージョン Ruby on Rails 4.0 **

フォーラム ソフトウェアは多くのカテゴリを持つことができ、これらのカテゴリ内に複数のフォーラムを持つことができます。

メイン ページは次のようになります。

カテゴリー1

  • フォーラム 1
  • フォーラム 2

カテゴリー2

  • フォーラム 3
  • フォーラム 4
  • フォーラム5

フォーラムを作成するときは、フォーラムを配置するカテゴリを選択できるドロップダウン メニューが必要です。

最初に、2 つの異なるスキャフォールドを作成しました。1 つはカテゴリ用、もう 1 つはフォーラム用です。2つを接続するために外部キーを使用しました。これが最善の方法かどうかはわかりませんが、まったく対話させることができませんでした。私は自分のコードをひどく台無しにしてしまったので、それを示すことはほとんどありません。

Rails4 のサブカテゴリの追加とソリューションのカテゴリとサブカテゴリのモデル レールを使用してみましたが、どちらもエラーが発生しました。

これが私のコードの一部です。大したことではありませんが、どこから始めればよいか教えていただけないでしょうか。これを行うためのより良い方法 (2 つのテーブルを使用しない) があれば、お知らせください。gemを使わずにこれを行う最善の方法を聞きたいです

警告: 私のコードは完全に混乱しています。

移行

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

フォーラム コントローラ(カテゴリに接続できる何かが不足していることはわかっていますが、それが何かはわかりません)

class 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>
4

1 に答える 1