【问题标题】:Rails Rendering Show in Bootstrap Modal from index page索引页面中的引导模式中的 Rails 渲染显示
【发布时间】:2015-10-27 21:33:55
【问题描述】:

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

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

    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吗?如果没有,您使用的是什么浏览器?如果是这样,您的浏览器控制台上是否会出现任何错误?
  • @neanderslob - 我正在使用 Firefox,但 js 源代码中没有显示错误。打开 localhost:3000/photos#showModal 时,“网络”中也不会记录任何更改。依然显示Request URL:localhost:300/photosRequest method: Get Status code: 200 OK

标签: javascript jquery ruby-on-rails twitter-bootstrap carrierwave


【解决方案1】:

已解决:不是 ruby​​ 的 javascript 问题。

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

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

【讨论】: