如何使用单个部分渲染集合中的每个对象?

时间:2017-06-06 17:59:30

标签: ruby-on-rails model-view-controller collections

我有一个工作部分用于我的Quote类的实例_quote.html.erb,我想使用这个相同的部分来呈现索引操作提供的集合中的每个对象。然而,我陷入困境,再也看不到我的错误。实例变量命名问题,控制器crud动作格式问题?代码如下:

quotes_controller.rb

class QuotesController < ApplicationController
  before_action :authenticate_user!, only: [ :new, :create, :edit, :update, :destroy ]
  # before_action :owners_only, only: [ :show, :edit, :index, :update, :destroy ]

  def new
    @quote = Quote.new
  end

  def create
    # @quote = Quote.new(quote_params)
    @quote = current_user.quotes.new(quote_params)
    if @quote.save
        redirect_to quote_url(@quote), notice: 'Quote request created'
    else
      render :new
    end
  end

  def show
    # @quote = Quote.find(params[:id])
    @quote = current_user.quotes.find(params[:id])
  end

  def index
    @quotes = current_user.quotes.all
  end

  def edit
    @quote = current_user.quotes.find(params[:id])
  end

  def update
    @quote = current_user.quotes.find(params[:id])
    if @quote.update_attributes(quote_params)
      redirect_to quote_path(@quote)
    else
      render :edit
    end
  end

  def destroy
    @quote = current_user.quotes.find(params[:id])
    @quote.destroy
    redirect_to quotes_path
  end

private

  def quote_params
    params.require(:quote).permit(:gla, :prev_cover, :co_name, :co_number, :postcode, :industry, :lives_overseas, 
                                  :scheme_start_date, :payment_frequency, :commission_level)
  end
end

_quote.html.erb

<section id="quote">
<div class="container">
<div class="row">
    <div class="col-md-4 text-center">
    <div class="panel panel-success panel-quote">
      <div class="panel-heading">
        <strong>GLA</strong>
        </div>
      <div class="panel-body text-center">
         <p><strong>Quote ID; <%= @quote.id %></strong></p>
      </div>

      <table class="table table-bordered">
        <tr>
          <td>Company name</td>
          <td><%= @quote.co_name %></td>
        </tr>
        <tr>
          <td>Company number</td>
          <td><%= @quote.co_number %></td>
        </tr>
        <tr>
          <td>Office postcode</td>
          <td><%= @quote.postcode %></td>
        </tr>
        <tr>
          <td>Industry</td>
          <td><%= @quote.industry %></td>
        </tr>
        <tr>
          <td>Previous cover</td>
          <td><%= @quote.prev_cover %></td>
        </tr>
        <tr>
          <td>Lives overseas</td>
          <td><%= @quote.lives_overseas %></td>
        </tr>
        <tr>
          <td>Scheme start date</td>
          <td><%= @quote.scheme_start_date %></td>
        </tr>
        <tr>
          <td>Payment frequency</td>
          <td><%= @quote.payment_frequency %></td>
        </tr>
        <tr>
          <td>Commission level</td>
          <td><%= @quote.commission_level %></td>
        </tr>
      </table>
    </div>
  </div>
</div>

index.html.erb

<% @quotes.each do |q| %>
  <%= render :partial => "quote", :collection => q %>
<% end %>

我在这里做了一件非常明显的事情,有人可以为我指出,盯着这么久我再也看不到了!

如果我跑:

<% @quotes.each do |q| %>
  <%= q.co_name %>
<% end %>

索引页面可以很好地呈现co_name值,表明集合确实可以通过。

1 个答案:

答案 0 :(得分:2)

我想这个:

<% @quotes.each do |q| %>
  <%= render :partial => "quote", :collection => q %>
<% end %>

可能更像这样:

<% @quotes.each do |quote| %>
  <%= render :partial => "quote", locals: {quote: quote} %>
<% end %>    

然后在你的部分,在顶部,做一些像:

<% @quote = local_assigns[:quote] %>
<section id="quote">
<div class="container">
<div class="row">
    <div class="col-md-4 text-center">
    <div class="panel panel-success panel-quote">
      <div class="panel-heading">
        <strong>GLA</strong>
        </div>
      <div class="panel-body text-center">
         <p><strong>Quote ID; <%= @quote.id %></strong></p>
      </div>

      <table class="table table-bordered">
        <tr>
          <td>Company name</td>
          <td><%= @quote.co_name %></td>
        </tr>
        ...
      </table>
    </div>
  </div>
</div> 

然后我想你会在路上。