1

我正在使用模态在索引页面内弹出显示页面.....一切正常,直到我点击一个产品。出于某种原因,模态框上的每个产品都会弹出相同的名称

我知道这是一个简单的解决方法,请帮助....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>

模型

产品.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
4

1 回答 1

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

这应该做的工作。

我还看到您没有使用从索引操作传递的局部变量。您正在使用可能是一个问题的@product,请尝试仅使用从 index.html.erb 传递的“产品”

于 2013-03-21T08:27:50.877 回答