has_many:through关系中的视图 [英] Views in a has_many :through relationship

查看:69
本文介绍了has_many:through关系中的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经对此进行了一段时间的修改,似乎无法弄清楚.这可能很简单,但是可以这样:

I've been tinkering with this for some time now, and can't seem to figure this out. It's probably something simple, but here goes:

我在层压板"和标准"之间具有has_many的低谷关系,并加入了模型标准化".

I have a has_many :trough relationship between 'laminate', and 'standards' with a joined model 'standardization'.

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

这种情况是层压板可以属于多个标准,并且我在视图的新部分中使用了所有功能-复选框和所有功能.我的问题是尝试显示给定层压板的相应标准的名称时.到目前为止,我可以显示层压板分配给哪些标准,但不能仅显示标准名称.

The scenario is that laminates can belong to several standards, and I've got everything working in the new-part of the view - checkboxes and everything. My problem is when trying to display the names of the corresponding standards for a given laminate. As of now I'm able to display which standards the laminates are assigned to, but not ONLY the names of the standards.

我的show.html.erb说:

My show.html.erb says:

<%= @laminate.standards %>

这将返回所有正确的信息,但会说

And this returns everything correct, but saying

 <%= @laminate.standards.name %>

...不起作用. 究竟该如何分配分配给每个人的标准名称?

... does not work. How on earth can I tap into the names of each individual, assigned standard?

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

推荐答案

以下@laminate.standards返回标准列表.此处的呼叫名称不起作用,您应该在列表上进行每个循环:

The following @laminate.standards return a list of Standards. Call name here can't work, you should do a each loop on the list:

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

如果您想使用较短的版本,但自定义程度较低:

If you want a shorter version but less customizable:

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

这篇关于has_many:through关系中的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆