多对多关系无法在视图上正确显示/保存

时间:2013-07-11 23:45:20

标签: ruby-on-rails many-to-many rails-activerecord

我在工作和类别之间有很多关系。我想在work#index页面上显示每件作品的类别名称。

不幸的是它只是空的[]: example of blank boxes

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

enter image description here

和多种选择的形式

 <%= 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 %>

我错过了一步吗?它不节约吗?这是我的第一个铁轨项目;因此,我第一次建模。我希望本文档的其余部分有助于解决这个问题。




以下是我将我的作品添加到作品中的方法: adding categories to works




我有三种模式:工作,类别,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中建立了多对多关系:

many to many assoication Rails cast

drop down menu

3 个答案:

答案 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