2016-04-13 10 views
1

Ben raylar üzerinde ruby ​​kullanarak 4.2.5 ve filterrific ve 3.0.6 taşlar olacaktır. Ama herhangi bir onay kutusunu işaretlediğimde javascript bozuk ve çalışmıyor.javascript kullanıyorum filtre raptiyen ve raylar üzerinde yakut içinde will_paginate

onun benim js dosyası

$('.spin span:last-child').click(function(){ 
    inputValue = parseInt($(this).parent().find($('.custom-input')).val()); 
    inputValue += 1 
    $(this).parent().find($('.custom-input')).val(inputValue); 
    unitPrice = parseFloat($(this).closest('.product').find('.unit-price span').text().substring(3)); 
    subTotal = parseFloat(inputValue * unitPrice).toFixed(2); 
    $(this).closest('.product').find('.total-price span').text(subTotal); 

});

onun benim index.html.haml sıralama ürünlerim

.flex-container.products.wrapper.no-flex 
.flex-container.relative 
    = form_for_filterrific @filterrific do |f| 
    .flex-item.sort-items< 
    Sort for 
    .select-style 
     = f.select(:sorted_by, @filterrific.select_options[:sorted_by], id: 'sort-select', include_blank: 'Seleccionar') 
    .filter-items.panel.panel-body 
    %h3.no-margin Mostrar por 
    %h4< 
     Type 
     %i.fa.fa-chevron-down> 
     %i.fa.fa-chevron-up 
    %ul 
     - @filterrific.select_options[:with_brand].each do |brand| 
     %li 
      = f.check_box :with_brand, { multiple: true, id: "filterrific_#{brand.name}", class: 'custom-checkbox' }, brand.id 
      = f.label brand.name, class: 'custom-label' 
    %h4< 
     Category 
     %i.fa.fa-chevron-down> 
     %i.fa.fa-chevron-up 

benim products_controller.rb

class ProductsController < ApplicationController 
def index 
@filterrific = initialize_filterrific(
    DistributorProduct, 
    params[:filterrific], 
    select_options: { 
    sorted_by: DistributorProduct.options_for_sorted_by, 
    with_brand: Brand.select(:id, :name), 
    with_category: Category.select(:id, :name), 
    with_distributor: Distributor.select(:id, :name), 
    with_presentation: Presentation.select(:id, :name) 
    }, 
    persistence_id: false 
) || return 
@distributor_products = @filterrific.find.page(params[:page]).search_query(params[:category], params[:search]).includes(:product) 
@order_item = current_order.order_items.new 
@brands = Brand.all 
@categories = Category.all 
@distributors = Distributor.all 
@presentations = Presentation.all 
end 
end 

ve So

class DistributorProduct < ActiveRecord::Base 
filterrific(available_filters: [ 
      :sorted_by, 
      :search_query, 
      :with_brand, 
      :with_category, 
      :with_distributor, 
      :with_presentation]) 

    self.per_page = 9 

belongs_to :distributor 
belongs_to :product 
has_many :sub_order_items 
has_many :order_items 

scope :search_query, lambda { |field, query| 
return nil if query.blank? 
query = "%#{query.downcase}%" 
k, value = field.split('-') 
key = k == 'sc' ? 'sub_categories' : 'categories' 
joins(product: { categories: :sub_categories }).where(key => { id: value.to_s }).where('LOWER(products.name) LIKE ?', query).distinct 
} 

scope :sorted_by, lambda { |sort_option| 
direction = (sort_option =~ /desc$/) ? 'desc' : 'asc' 
case sort_option.to_s 
when /^created_at_/ 
    order("distributor_products.created_at #{direction}") 
when /^name_/ 
    order("distributor_products.name #{direction}") 
when /^unit_price_/ 
    order("distributor_products.unit_price #{direction}") 
else 
    raise(ArgumentError, "Invalid sort option: #{sort_option.inspect}") 
end 
} 

scope :with_brand, lambda { |brand_id| 
return nil if brand_id.uniq == ['0'] 
joins(:product).where(products: { brand_id: brand_id }) 
} 

scope :with_category, lambda { |category_id| 
return nil if category_id.uniq == ['0'] 
joins(product: :categories).where(categories: { id: category_id }) 
} 

scope :with_distributor, lambda { |distributor_id| 
return nil if distributor_id.uniq == ['0'] 
where(distributor_id: distributor_id) 
} 

scope :with_presentation, lambda { |presentation_id| 
    return nil if presentation_id.uniq == ['0'] 
    joins(:product).where(products: { presentation_id: presentation_id }) 
} 

def self.options_for_sorted_by 
[ 
    ['Nombre (a-z)', 'name_asc'], 
    ['Nombre (z-a)', 'name_desc'], 
    ['Precio (menor a mayor)', 'unit_price_asc'], 
    ['Precio (mayor a menor)', 'unit_price_desc'] 
    ] 
end 

def image_url 
    "https://s3-sa-east-1.amazonaws.com/riqraops/catalogo/#{image}.png" 
end 
end 

benim distributor_product.rb modeli, ben hiçbir var bir onay kutusu seçildiğinde js neden çalışmıyor, bunun için sayfayı yeniden yüklemeniz gerekir: /, thx için tepkiler.

cevap

2

Nedeni turbolinks olabilir. senin Gemfile bu eklemeyi deneyin:

gem 'jquery-turbolinks' 

Sonra bundle install çalıştırın ve bu sırada sizin application.js ekleyin:

//= require jquery 
//= require jquery.turbolinks 
//= require jquery_ujs 
// 
// ... your other scripts here ... 
// 
//= require turbolinks 

sunucu yeniden başlatın ve tekrar deneyin. Umarım bu size yardımcı olur.

İlgili konular