Ruby on rails 3 render show页面在索引页面内,如何在每个模式上放置不同的id

Ruby on rails 3 render show页面在索引页面内,如何在每个模式上放置不同的id,ruby-on-rails-3,modal-dialog,partial,Ruby On Rails 3,Modal Dialog,Partial,我正在使用一个模式在索引页面内弹出显示页面……在我单击某个产品之前,一切正常。出于某种原因,modal上的每个产品都会出现相同的名称 我知道这很容易修复,请帮助我…rails新手 这是我的代码: 视图 _show.html.erb <div id="myModal" class="modal hide fade" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="cont

我正在使用一个模式在索引页面内弹出显示页面……在我单击某个产品之前,一切正常。出于某种原因,modal上的每个产品都会出现相同的名称

我知道这很容易修复,请帮助我…rails新手 这是我的代码:

视图

_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>

'products/show',:locals=>{:product=>product}%>
型号

product.rb

class Product < ActiveRecord::Base
  attr_accessible :name, :photo
end
类产品
控制器

产品\u控制器.rb

class ProductsController < ApplicationController

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


  def index
    @products = Product.all
  end

end
class ProductsController
好,问题在于此代码

在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>

应该是

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

并将ur_show.html.erb更改为此

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

这应该可以完成任务


我还看到您没有使用从索引操作传递的局部变量。您正在使用@product,这可能是一个问题,请尝试仅使用从index.html.erb传递的“product”,那么对于索引,代码会像这样吗?如果我有100多种产品……我会把它们都列出来吗@Abibullah Rahamathulahhref=#mymodel_1,id=1的show.html.haml中应该存在相同的id,我想我在这里遇到了类似的问题。。。。。。你能帮我一把吗@Abibullah Rahamathulah
<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>