-1

我看着这个railscast

使用它作为模板我在我的 rails 应用程序中使用数据表、引导程序和 will_paginate。

我的模型名为“Demo”,代码如下:

应用程序.js

 ...............
 //= require jquery
 //= require jquery_ujs
 //= require twitter/bootstrap
 //= require turbolinks
 //= require dataTables/jquery.dataTables
 //= require_tree .

demos.js.coffee

 jQuery -> 
     $("#demos").dataTable
      sPaginationType: "full_numbers" 
      bJQueryUI: true
      bProcessing: true
      bServerSide: true
      sAjaxSource: $('#demos').data('source')

我在项目中添加了一个名为“ModelDatatables”的类,以便让任何模型在其视图中使用数据表。类 ModelsDatatable 委托 :params, :link_to, :to => :@view attr_accessor :model_name, :models

   def initialize(view, model_name)
     puts "nitializing Model Datatables with modelname = #{model_name}"
 @view = view
 @model_name = model_name
   end

   def as_json(options = {})
   {
      sEcho: params[:sEcho].to_i,       #variable needed by Datatables server
      iTotalRecords: @model_name.count, #variable needed by Datatables server
      iTotalDisplayRecords: models.total_entries,
      aaData: data
    }
    end

     private #====================================================================

    def data
     models.map do |m|
       [
        link_to(m.name, m)
       ]
      end
    end

  def models
     @models ||= fetch_models
   end

   def fetch_models
      mod_instance = @model_name.order("#{sort_column} #{sort_direction}")
     mod_instance = mod_instance.page(page).per_page(per_page)
     if params[:sSearch].present?
       mod_instance = mod_instance.where("name like :search or category like :search",            
         search: "%#{params[:sSearch]}%")
     end
     mod_instance
   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
att_array = @model_name.attribute_names.to_a
    columns = att_array
    columns[params[:iSortCol_0].to_i]
  end

 def sort_direction
    params[:sSortDir_0] == "desc" ? "desc" : "asc"
 end
end

在我的控制器中,我尝试像这样使用数据表类 Demo_controller.rb

 def index
respond_to do |format|
     format.html
     format.json { render json: ModelsDatatable.new(view_context, Demo ) }
    end
 end

意见:

演示/index.html.erb

 <%- model_class = Demo -%>
 <div class="page-header">
   <h1><%=t '.title', :default => model_class.model_name.human.pluralize.titleize %></h1>
 </div>
 <table class="table table-striped display" id="demos" data-source="<%= demos_url(format: "json") %>" >
   <thead>
     <tr>
       .......
       </thead>
     <tbody>
          .......
     </tbody>
     </table>

当我在浏览器上运行它时。我收到此错误:

 SyntaxError: unexpected {
   (in /Users/Xxxxxx/Desktop/xxxxxxxxx/app/assets/javascripts/demos.js.coffee)

  Extracted source (around line #37):
                 <%= javascript_include_tag "application" %>

  app/views/layouts/application.html.erb:37:in   
     `_app_views_layouts_application_html_erb__2549661732362878565_70299008379240'
      app/controllers/demos_controller.rb:8:in `index'

不知道下一步该怎么做。有人可以帮忙吗?

谢谢

4

1 回答 1

0

看起来你有一个

语法错误:意外 {

一定要检查你在 demo.js.coffee 中的空格,我刚刚编辑了你的问题,假设你复制粘贴,你混合了空格和制表符。

于 2013-11-05T23:59:35.703 回答