Rails Rendering从索引页以Bootstrap模式显示

Rails Rendering Show in Bootstrap Modal from index page

本文关键字:Bootstrap 模式 显示 Rendering 索引 Rails      更新时间:2023-09-26

我正试图通过Bootstrap Modal从照片索引页面渲染"show"。如果我直接使用显示页面(..'views'photos'show.html.erb)而不通过引导模式渲染,则预期的照片(使用carrierwave未加载)会正确显示。

但问题是,当我尝试用_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

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