我在工作和类别之间有多对多的关系。我想在work#index
页面上显示每个作品的类别名称。
不幸的是它只是空的[]:
作品#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(连接表)
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
据我了解,我将这两行添加到我的控制器的 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 中创建了多对多关系: