在索引页面内渲染显示页面,如何在每个模态上放置不同的id

时间:2013-03-21 08:16:39

标签: ruby-on-rails-3 modal-dialog partial

我正在使用Modal弹出索引页面内的显示页面.....一切正常,直到我点击产品。出于某种原因,模式

上的每个产品都会出现相同的名称

我知道这是一个简单的解决方案,请帮助....新的铁路 这是我的代码:

视图

_show.html.erb

<div id="myModal" class="modal hide fade" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="content-inner hero-unit">
    <h1 class="pump-up center">
      <br>
      <strong>Coming Soon.</strong></h1>
      <br><br>
      <p>
        <b>Name: </b>
        **<%= @product.name %>**
      </p>
  </div>
</div>

index.html.erb

<div class="row">
  <% @products.each do |product| %>
      <div class="span3">

      <%= render :partial => 'products/show', :locals => { :product => product }  %>
        <a href="#myModal" role="button" data-toggle="modal">
        <%=(image_tag product.photo(:medium))%></a>

      </div>
  <% end %>
</div>

模型

product.rb

class Product < ActiveRecord::Base
  attr_accessible :name, :photo
end

控制器

products_controller.rb

class ProductsController < ApplicationController

  def show
    @product = Product.find(params[:id])
  end


  def index
    @products = Product.all
  end

end

1 个答案:

答案 0 :(得分:3)

好问题是这个代码。

在Index.html.erb

应该是

<a href="#myModal<%=product.id %>" role="button" data-toggle="modal">

并将您的_show.html.erb更改为此

<div id="myModal<%=product.id%>" class="modal hide fade" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">

这应该是JOB。

此外,我发现您没有使用从索引操作传递的局部变量。您使用的@product可能是个问题,请尝试使用从index.html.erb传递的'product'

相关问题