如何使用Rails 3和jQuery在Facebox中渲染部分

时间:2011-08-31 14:06:38

标签: jquery ruby-on-rails-3 partial facebox

我正在尝试使用Facebox插件为jQuery在模态窗口中渲染部分,但是当模态出现时,我首先看到我希望从我的index.js.erb文件中看到的内容,但随后立即关注是来自同一文件的原始代码。 (screenshot)我做错了什么?

的routes.rb

resources :brands, :only => [:index, :show], :via => [:get] do
  resources :promotions, :controller => 'brand_promotions', :only => [:index], :via => [:get] do
    collection do
      get :index
    end
  end
end

应用/视图/品牌/ show.html.erb

<% content_for :head do %>
  <%= stylesheet_link_tag 'facebox', :media => 'screen, projection' %>
  <%= javascript_include_tag 'facebox' %>
  <script type="text/javascript">
    $(document).ready(function() {  
      $('#show-all-promotions').facebox({  
        loadingImage : '/images/loading.gif',  
        closeImage   : '/images/closelabel.png',  
      });  
    });
  </script>
<% end %>
...
<section id="brand-promotions">
  <header>
    <h3>Featured Savings</h3>
    <% if @brand.promotions.count > @promotions.count -%>
      <%= link_to 'View all ' + pluralize(@brand.promotions.count, 'promotion'), brand_promotions_path(@brand), :class => 'right', :style => 'margin-top: -1.5em', :id => 'show-all-promotions' %>
    <% end -%>
  </header>

  <% if !@promotions.empty? -%>
    <%= render :partial => 'brand_promotions/promotions', :locals => { :promotions => @promotions } %>
  <% else -%>
    <p><%= @brand.name %> has not posted any promotions... yet!</p>
  <% end -%>
</section>

应用/视图/ brand_promotions / _promotions.html.erb

<ul class="promotions">
  <% promotions.each_with_index do |promotion, index| -%>
    <% if index == 0 &&  promotion.format == BrandPromotion::FORMATS[:wide] -%>
      <li class="span-15 last promotion">
    <% else -%>
      <li class="span-7 <%= cycle('first', 'push-1 last', :name => 'promotion_styles') %> promotion">
    <% end -%>
      <%= image_tag promotion.image.small.url, :class => 'promo_image' %>
      <div class="promotion_container">
        <% if !promotion.price.blank? %>
          <span class="promotion-price"><%= raw promotion.price %></span>
        <% end %>
        <h4><%= raw promotion.name %></h4>
        <p><%= raw promotion.content %></p>
      </div>
    </li>
  <% end -%>
</ul>

应用/控制器/ brand_promotions_controller.rb

def index
  @brand = Brand.find(params[:brand_id])
  @promotions = @brand.promotions.all
  @page_title = "Savings from #{@brand.name}"
  respond_to do |format|
    format.html # index.html.erb
    format.js {render :layout => false} 
  end
end

应用/视图/ brand_promotions / index.js.erb的

$('#facebox .content').html("<%= escape_javascript(render :partial => 'promotions', :locals => { :promotions => @promotions }) %>");

公开/ Javascript角/ application.js中

// This seems to be required based on 
// https://github.com/rails/jquery-ujs/commit/fbbefa0773d791d3a67b7a3bb971c10ca750131b
$(function() {
  jQuery.ajaxSetup({
    beforeSend: function(xhr) {
      xhr.setRequestHeader("Accept", "text/javascript");
    }
  });
});

0 个答案:

没有答案