我在工作和类别之间有很多关系。我想在work#index
页面上显示每件作品的类别名称。
不幸的是它只是空的[]:
Works#index markup
<% @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 %>
我错过了一步吗?它不节约吗?这是我的第一个铁轨项目;因此,我第一次建模。我希望本文档的其余部分有助于解决这个问题。
以下是我将我的作品添加到作品中的方法:
我有三种模式:工作,类别,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
据我了解,我将这两行添加到我的控制器的新编辑中,以便将它们保存到集合中。
@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中建立了多对多关系:
答案 0 :(得分:1)
您需要在accepts_nested_attributes_for :categoryworks
模型中加入Works
。
见http://guides.rubyonrails.org/form_helpers.html#configuring-the-model
同样work.categories
显示Category
,因为它返回一个数组。它应该是work.categories.join(', ')
一旦您解决了表单问题,请尝试使用。
答案 1 :(得分:0)
不会<td><%= work.category.name %></td>
这样做吗?
修改强>
您的work
模型应该说has_many :categories
,而不是has_many :category
。
class Work < ActiveRecord::Base
attr_accessible :name, :subtitle
validates :name, :presence => true
has_many :categoryworks
has_many :categories, :through => :categoryworks
end
答案 2 :(得分:0)
<强>解强> 我没有更新works_controller里面的def创建
params[:works][:id].each do |work|
if !work.empty?
@category.categoryworks.build(:work_id => work)
end
end