私はこれをしばらくいじっていましたが、これを理解できないようです。単純なことかもしれませんが、次のようになります。
「ラミネート」と「標準」の間に has_many :trough 関係があり、モデル「標準化」が結合されています。
標準.rb
class Standard < ActiveRecord::Base
attr_accessible :description, :name
has_many :standardizations
has_many :laminates, :through => :standardizations
end
標準化.rb
class Standardization < ActiveRecord::Base
attr_accessible :laminate_id, :standard_id
belongs_to :laminate
belongs_to :standard
end
ラミネート.rb
class Laminate < ActiveRecord::Base
attr_accessible :name, :standard_ids
has_many :standardizations
has_many :standards, :through => :standardizations
end
シナリオは、ラミネートがいくつかの標準に属することができ、ビューの新しい部分ですべてが機能することです-チェックボックスとすべて. 私の問題は、特定のラミネートに対応する規格の名前を表示しようとするときです。現在、ラミネートが割り当てられている規格を表示できますが、規格の名前だけではありません.
私の show.html.erb は言う:
<%= @laminate.standards %>
そして、これはすべて正しいものを返しますが、
<%= @laminate.standards.name %>
... 動作しません。いったいどうすれば、割り当てられた標準の各個人の名前を利用できるでしょうか?
ラミネートコントローラー:
class LaminatesController < ApplicationController
# GET /laminates
# GET /laminates.json
def index
@laminates = Laminate.all
@standards = Standard.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @laminates }
end
end
# GET /laminates/1
# GET /laminates/1.json
def show
@laminate = Laminate.find(params[:id])
@standard = Standard.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @laminate }
end
end
# GET /laminates/new
# GET /laminates/new.json
def new
@laminate = Laminate.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @laminate }
end
end
# GET /laminates/1/edit
def edit
@laminate = Laminate.find(params[:id])
end
# POST /laminates
# POST /laminates.json
def create
@laminate = Laminate.new(params[:laminate])
respond_to do |format|
if @laminate.save
format.html { redirect_to @laminate, notice: 'Laminate was successfully created.' }
format.json { render json: @laminate, status: :created, location: @laminate }
else
format.html { render action: "new" }
format.json { render json: @laminate.errors, status: :unprocessable_entity }
end
end
end
# PUT /laminates/1
# PUT /laminates/1.json
def update
@laminate = Laminate.find(params[:id])
respond_to do |format|
if @laminate.update_attributes(params[:laminate])
format.html { redirect_to @laminate, notice: 'Laminate was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @laminate.errors, status: :unprocessable_entity }
end
end
end
# DELETE /laminates/1
# DELETE /laminates/1.json
def destroy
@laminate = Laminate.find(params[:id])
@laminate.destroy
respond_to do |format|
format.html { redirect_to laminates_url }
format.json { head :no_content }
end
end
end