Rails 在索引页面的 Bootstrap 模态中呈现显示

Rails Rendering Show in Bootstrap Modal from index page

我正在尝试通过 Bootstrap 模式从照片索引页面呈现“显示”。 如果我直接使用显示页面 (..\views\photos\show.html.erb) 而不通过 bootstrap 模式渲染,则预期的照片(使用载波卸载)会正确显示。

但问题是当我尝试使用 _show.html.erb. 渲染时照片不显示,但是即使 _show.html.erb. 中有任何可用的硬编码文本,后备图像也会显示 以下是片段;

    views\photos\_show.html.erb

`<% if @photo.image? %>
  <%= image_tag @photo.image.url(:thumb2) %> #this is the intended photo
  <% else %>
  <%= image_tag("fallback/default.png") %>
  <% end %>`

    Photos_controller.rb

`def show
  @photo = Photo.find(params[:id])
  respond_to do |format|
    format.html # show.html.erb
    format.xml  { render :xml => @photo }
  end
end`


`views\photos\index.html.erb`  for Bootstrap modal

    <div class =”..”>
    <% if photo.image? 
    <%# Added Bootstrap data modal attribute %>
    <%= link_to (image_tag photo.image.url(:thumb)), '#showModal', 'data-toggle' => 'modal' %>

    <div class="modal fade" id="showModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-dialog modal-lg">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h4 class="modal-title" id="myModalLabel">Showing photo</h4>
          </div>
          <div class="modal-body">
            <%= render 'show' %>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          </div>
        </div>
      </div>
    </div>

photo.js

 $("#showModal").click(function(){
    $("#show").modal();
    });

已解决:不是 javascript 问题而是 ruby。

http://api.rubyonrails.org/classes/ActionView/PartialRenderer.html

在索引页面中,我将 <%= render 'show' %> 更改为 <%= render :partial => 'show', :locals => { :photo => 照片 } %> 然后将 _show.html.erb 中的@photo 更改为 photo,使其成为索引页中的局部变量。