仕事とカテゴリーの間に多対多の関係があります。各作品のカテゴリ名をページに表示したいwork#index
。
残念ながら、空の [] として表示されます。
Works#index マークアップ
<% @works.each do |work| %>
<tr>
<td><%= work.name %></td>
<td><%= work.subtitle %></td>
<td><%= work.categories %></td>
<td><%= link_to 'Show', work %></td>
<td><%= link_to 'Edit', edit_work_path(work) %></td>
<td><%= link_to 'Destroy', work, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
または、マークアップを次のように変更しますwork.categories.name
および複数選択のフォーム
<%= fields_for(@work_category) do |cw| %>
<div class="field">
<%= cw.label "All Categories" %><br />
<%= collection_select(:categories, :id, @all_categories, :id, :name, {}, {:multiple => true}) %>
</div>
<% end %>
手順がありませんか?節約じゃないの?これは私の最初の Rails プロジェクトです。したがって、私の初めてのモデリング。このドキュメントの残りの部分がこの問題の解決に役立つことを願っています。
作品にカテゴリを追加する方法は次のとおりです。
work、category、categoryworks (結合テーブル)の3 つのモデルがあります。
class Category < ActiveRecord::Base
attr_accessible :description, :name
validates :name, :presence => true
has_many :categoryworks
has_many :works, :through => :categoryworks
end
class Work < ActiveRecord::Base
attr_accessible :name, :subtitle
validates :name, :presence => true
has_many :categoryworks
has_many :categories, :through => :categoryworks
end
class Categorywork < ActiveRecord::Base
attr_accessible :category_id, :work_id
belongs_to :category
belongs_to :work
end
私の理解では、これらの 2 行をコントローラーの new および edit に追加して、コレクションに保存できるようにします。
@all_categories = Category.all
@work_category = @work.categoryworks.build
の完全なコントローラーは次のworks_controller
とおりです。
class WorksController < ApplicationController
# GET /works
# GET /works.json
def index
@works = Work.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @works }
end
end
# GET /works/1
# GET /works/1.json
def show
@work = Work.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @work }
end
end
# GET /works/new
# GET /works/new.json
def new
@work = Work.new
@all_categories = Category.all
@work_category = @work.categoryworks.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: @work }
end
end
# GET /works/1/edit
def edit
@work = Work.find(params[:id])
@all_categories = Category.all
@work_category = @work.categoryworks.build
end
# POST /works
# POST /works.json
def create
@work = Work.new(params[:work])
respond_to do |format|
if @work.save
format.html { redirect_to @work, notice: 'Work was successfully created.' }
format.json { render json: @work, status: :created, location: @work }
else
format.html { render action: "new" }
format.json { render json: @work.errors, status: :unprocessable_entity }
end
end
end
# PUT /works/1
# PUT /works/1.json
def update
@work = Work.find(params[:id])
respond_to do |format|
if @work.update_attributes(params[:work])
format.html { redirect_to @work, notice: 'Work was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @work.errors, status: :unprocessable_entity }
end
end
end
# DELETE /works/1
# DELETE /works/1.json
def destroy
@work = Work.find(params[:id])
@work.destroy
respond_to do |format|
format.html { redirect_to works_url }
format.json { head :no_content }
end
end
end
2 つのビデオを通じて、Rails で多対多の関係を作成しました。