Как сделать рендеринг частичного в Facebox, используя Rails 3 и jQuery - PullRequest
1 голос
/ 31 августа 2011

Я пытаюсь использовать плагин Facebox для jQuery для рендеринга частичного в модальном окне, но когда появляется модальное, я сначала вижу то, что ожидаю увидеть из моего файла index.js.erb, но затем сразу жесырой код из того же файла.( снимок экрана ) Что я делаю не так?

rout.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

app / views / brand / 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>

app / views / 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>

app / controllers / 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

app / views / brand_promotions / index.js.erb

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

public / javascripts / 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");
    }
  });
});
...