1

インデックスページ内にショーページをポップアップするためにモーダルを使用しています.....製品をクリックするまで、すべてが正常に機能します。何らかの理由で、モーダルのすべての製品に同じ名前が表示されます

私はそれが簡単な修正であることを知っています、助けてください....レールの新機能これは私のコードです:

ビュー

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

1 に答える 1

3

OK問題はこのコードにあります。

Index.html.erb内

そのはず

<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」だけを使用してみてください

于 2013-03-21T08:27:50.877 に答える