【问题标题】:Views in a has_many :through relationshiphas_many :through 关系中的视图
【发布时间】:2013-01-31 16:38:13
【问题描述】:

我已经对此进行了一段时间的修改,但似乎无法解决这个问题。这可能很简单,但这里是:

我在“层压”和“标准”之间有一个 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

Laminate.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 %>

... 不起作用。 究竟如何才能利用每个人的姓名、指定的标准?

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

【问题讨论】:

    标签: ruby-on-rails ruby views many-to-many has-and-belongs-to-many


    【解决方案1】:

    以下@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
    

    【讨论】:

    • 是的,是的,这里也是!谢谢!
    猜你喜欢
    • 1970-01-01
    • 2015-07-03
    • 2017-01-03
    • 2011-04-27
    • 1970-01-01
    • 2011-12-24
    • 1970-01-01
    • 2011-01-06
    • 1970-01-01
    相关资源
    最近更新 更多