Web sitemde çalışabilmek için jQuery'nin datatable'ı almaya çalışıyorum ama Rails 4 üzerinde çalışmasını sağlayamıyorum. Sütunları sıralamaya çalıştığımda hata yaşıyorum: jquery.dataTables.self-770bf6f….js?body=1:2525 Uncaught TypeError: Cannot set property 'data' of null
İşte Raylar 4 + datatables: Sırala/arama çalışmaz
products_controller.rb
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
# GET /products
# GET /products.json
def index
respond_to do |format|
format.html
format.json { render json: ProductsDatatable.new(view_context) }
end
end
# GET /products/1
# GET /products/1.json
def show
end
# GET /products/new
def new
@product = Product.new
end
# GET /products/1/edit
def edit
end
# POST /products
# POST /products.json
def create
@product = Product.new(product_params)
respond_to do |format|
if @product.save
format.html { redirect_to @product, notice: 'Product was successfully created.' }
format.json { render :show, status: :created, location: @product }
else
format.html { render :new }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /products/1
# PATCH/PUT /products/1.json
def update
respond_to do |format|
if @product.update(product_params)
format.html { redirect_to @product, notice: 'Product was successfully updated.' }
format.json { render :show, status: :ok, location: @product }
else
format.html { render :edit }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
# DELETE /products/1
# DELETE /products/1.json
def destroy
@product.destroy
respond_to do |format|
format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_product
@product = Product.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def product_params
params.require(:product).permit(:name, :category, :price)
end
end
index.html.erb
<table id="products" data-source="<%= products_url(format: "json") %>">
<thead>
<tr>
<th>Name</th>
<th>Category</th>
<th>Price</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
products.coffee ben Railscasts aşağıdaki düzenlemeler # 340 ile kam oldukça uzun bir süre için mücadele sonra
jQuery ->
$('#products').DataTable
"Processing": true
"serverSide": true
"Ajax": $('#products').data('source')
product_datatable.rb
class ProductsDatatable
delegate :params, :link_to, :number_to_currency, to: :@view
def initialize(view)
@view = view
end
def as_json(options = {})
{
sEcho: params[:sEcho].to_i,
iTotalRecords: Product.count,
iTotalDisplayRecords: products.total_entries,
aaData: data
}
end
private
def data
products.map do |product|
[
product.name,
product.category,
product.price
]
end
end
def products
@products ||= fetch_products
end
def fetch_products
products = Product.order("#{sort_column} #{sort_direction}")
products = products.page(page).per_page(per_page)
if params[:sSearch].present?
products = products.where("name like :search or category like :search", search: "%#{params[:sSearch]}%")
end
products
end
def page
params[:iDisplayStart].to_i/per_page + 1
end
def per_page
params[:iDisplayLength].to_i > 0 ? params[:iDisplayLength].to_i : 10
end
def sort_column
# columns = [Drug.name, Drug.category, Drug.price]
# byebug
columns = %w[name category price]
columns[params[:iSortCol_0].to_i]
end
def sort_direction
params[:sSortDir_0] == "desc" ? "desc" : "asc"
end
end
çıkış bu bağlantı adam http://www.nikolatodorovic.com/nikolas-blog/rails- 4-datatables-110-ajax – uzaif
Çalışıyorum ve hala çalışamıyorum. Ben de onların HTML –
okuma zor bir zaman geçiriyorum ne sorunla yüzleşiyorsun? – uzaif