1

我一直在修補這個問題一段時間了,現在似乎無法解決這個問題。這可能是簡單的,但這裏有:has_many中的視圖:通過關係

我有一個has_many:「層壓」和「標準」之間的關係,加入模型'標準化'。

Standard.rb

class Standard < ActiveRecord::Base 
attr_accessible :description, :name 
has_many :standardizations 
has_many :laminates, :through => :standardizations 
end 

Standardization.rb

class Standardization < ActiveRecord::Base 
    attr_accessible :laminate_id, :standard_id 
    belongs_to :laminate 
    belongs_to :standard 
end 

Laminate.rb

class Laminate < ActiveRecord::Base 
attr_accessible :name, :standard_ids 
has_many :standardizations 
has_many :standards, :through => :standardizations 
end 

的風光ario是層壓板可以屬於幾個標準,並且我已經在視圖的新部分 - 複選框和所有東西中工作。我的問題是試圖顯示給定層壓板相應標準的名稱。到目前爲止,我能夠顯示層壓板分配給哪些標準,但不僅僅是標準的名稱。

我show.html.erb說:

<%= @laminate.standards %> 

而這一切都返回正確的,但他說

<%= @laminate.standards.name %> 

...不起作用。 我怎樣才能利用每個人的名字,分配標準?

Laminate_controller:

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 

回答

5

以下@laminate.standards返回標準的列表。這裏調用名稱不能工作,你應該做每個循環就行了:

# replace the following: 
<%= @laminate.standards.name %> 
# with this code: 
<% @laminate.standards.each do |standard| %> 
    <%= standard.name %> 
<% end %> 

如果你想有一個較短的版本,但少定製:

<%= @laminate.standards.map{ |standard| standard.name }.join(', ') %> 
# This will show all the standards' name with a coma-space ',' between it 

# same a above but shorter: 
<%= @laminate.standards.map(&:name).join(', ') %> 
# this will call the 'name' method on each standard of @laminate.standards 
# and join them with a coma-space 
# something that would look like this: 
# name1, name2, name3 
+1

是的,是的,是的!謝謝! – trymv

+0

是的,是的,在這裏也是!謝謝! – masaaki