0

我在工作和类别之间有多对多的关系。我想在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 中创建了多对多关系:

多对多关联 Rails cast

下拉式菜单

4

3 回答 3

1

You need to include accepts_nested_attributes_for :categoryworks in your Works model. See http://guides.rubyonrails.org/form_helpers.html#configuring-the-model

Also work.categories is displaying Category because its returning an array. It should be work.categories.join(', ') Try that once you fix the problem with your form.

于 2013-07-12T00:16:20.970 回答
0

解决方案 我没有更新works_controller内部的def create

params[:works][:id].each do |work|
  if !work.empty?
    @category.categoryworks.build(:work_id => work)
  end
end
于 2013-07-12T05:45:31.757 回答
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
于 2013-07-11T23:48:26.293 回答