现在我正在构建一个使用选择倍数接受多对多的表单。在创建表单后,我试图显示在SHOW页面中收集的信息但是我不知道如何在嵌入式ruby中显示该数据。连接模型是CardTypesList
模型
class Card < ActiveRecord::Base
self.inheritance_column = nil
validates :name, presence: true, uniqueness: {case_sensitive: false}
has_many :card_type_lists
has_many :card_types, through: :card_type_lists
accepts_nested_attributes_for :card_type_lists
end
class CardType < ActiveRecord::Base
has_many :card_type_lists
has_many :cards, through: :card_type_lists
end
class CardTypeList < ActiveRecord::Base
belongs_to :cards
belongs_to :card_types
accepts_nested_attributes_for :card_type
end
使用select
的新表单<%= f.label :types %>
<%= f.select :card_type_ids, CardTypes.all.collect{|x| [x.name, x.name]}, {},{:title => "Select a Type", :multiple => true, :class => 'selList'} %>
嵌入式红宝石试验
<td class="card-td"><%= @card.card_types %></td>
预期:[“Type 1”,“Type2”] 这会呈现页面,但会产生(文本中):
<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_CardTypes:0x00000102f58a18>
编辑:添加了架构。
ActiveRecord::Schema.define(version: 20140120042152) do
create_table "card_type_lists", force: true do |t|
t.integer "card_type_id"
t.integer "card_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "card_types", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "cards", force: true do |t|
t.string "name"
t.string "set"
t.string "card_types"
t.string "colors"
t.string "cost"
t.string "rarity"
t.string "oracle"
t.float "value"
t.integer "number_owned"
t.string "notes"
t.string "img_link"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "types_mask"
end
add_index "cards", ["name"], name: "index_cards_on_name", unique: true
end
这是我的控制者:
class CardsController < ApplicationController
def new
@card = Card.new
@card.card_type_lists.build.build_card_type
end
def show
@card = Card.find(params[:id])
end
def create
@card = Card.new(card_params)
if @card.save
redirect_to @card
else
render 'new'
end
end
private
def card_params
params.require(:card).permit(:name, :set, {:card_types => []}, :color, :cost, :rarity,:oracle,:value, :number_owned,:notes)
end
end
答案 0 :(得分:0)
应该是:
class CardTypeList < ActiveRecord::Base
belongs_to :card
belongs_to :card_type #Singular
end
还有一些事情要考虑:
accepts_nested_attributes_for
<强>模式强>
加入has_many :through
中的模型必须foreign_key
引用他们加入的两个模型。你这样做的方法是使用这样的架构:
card_types_lists
id | card_id | card_type_id | other | information | created_at | updated_at
当您提到错误no such column: card_type_lists.card_types_id
时,通常意味着您在数据库中没有正确的列,或者您的引用不正确。看着它,它是你的关联(引用复数而不是单数)(在上面修复)
<强>表单强>
您应该考虑使用accepts_nested_attributes_for
将正确的数据发送到嵌套模型
这是您希望在任一模型中创建某些记录,并允许您为父模型中的其他模型定义“新”对象,将数据传递给您的子模型,像这样:
#app/models/card.rb
Class Card < ActiveRecord::Base
has_many :card_type_lists
has_many :card_types, through: :card_type_lists
accepts_nested_attributes_for :card_type_lists
end
#app/models/card_type_list.rb
Class CardTypeList < ActiveRecord::Base
belongs_to :card
belongs_to :card_type
accepts_nested_attributes_for :card_type
end
#app/controllers/cards_controller.rb
def new
@card = Card.new
@card.card_types_lists.build.build_card_type
end
数据强>
如果要正确显示关联数据,请尝试以下操作:
#app/views/cards/show.html.erb
<%= @card.card_types %>