【发布时间】:2018-08-20 21:34:26
【问题描述】:
我正面临这个问题,我试图测试只是为了从用户模型中获取结果。
NoMethodError(索引中未定义的方法keys' for true:TrueClass):
app/controllers/handymen_controller.rb:12:inblock(2级)'
app/controllers/handymen_controller.rb:9:in `index'
这是控制器
def index
#@handymen = Handyman.search(params[:profession])
line 9 respond_to do |format|
format.html
line 12 format.json {render json: HandymanDatatable.new(view_context)}
end
end
这是数据表
class HandymanDatatable < AjaxDatatablesRails::Base
def view_columns
# list of the model(s) columns mapped to the data we need to present
# Declare strings in this format: ModelName.column_name
# or in aliased_join_table.column_name format
@view_columns ||= {
id: { source: "User.id", cond: :like, searchable: true, orderable: true},
first_name: { source: "User.first_name"},
}
Rails.logger.debug("view_columns 123")
结束
def data
records.map do |record|
{
id: record.id,
first_name: record.first_name,
DT_RowId: record.id,
}
end
end
def get_raw_records
Rails.logger.debug("this is the dataTable 123")
byebug
User.where(id: 7)
结束
end
这是 Datatable 的 javascript 文件
jQuery(document).ready(function() {
$('#handymen-datatable').dataTable({
"processing": true,
"serverSide": true,
"ajax": $('#handymen-datatable').data('source'),
"pagingType": "full_numbers",
"columns": [
{"data": "id"},
{"data": "first_name"}
]
// pagingType is optional, if you want full pagination controls.
// Check dataTables documentation to learn more about
// available options.
}); });
这是模型
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable, :lockable
belongs_to :profile, :polymorphic => true
def handyman?
profile_type == "Handyman"
结束 结束
【问题讨论】:
-
你能告诉我们 view_context 是在哪里定义的吗?
-
@Rigo view_context
标签: ruby-on-rails datatables rubygems ruby-on-rails-5