-
Notifications
You must be signed in to change notification settings - Fork 227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using in API only mode? #240
Comments
Can you please try with : def index
respond_to do |format|
format.json { render json: UserDatatable.new(view_context).to_json }
end
end |
@n-rodriguez Same output. |
what if? def index
render json: UserDatatable.new(view_context)
end |
@ajahongir Same output. |
well. try this? def index
data = {id: 1, name: 'test'}
render json: data
end |
@ajahongir works as expected
|
so this also not working? def index
render json: UserDatatable.new(view_context).to_json
end did you override to_json method on your datatable? |
@ajahongir Nope. That doesn't work. I didn't override
My guess is that, this has something to do with |
Not sure if `view_context` works in a API mode, as there is no `view`
provided.
[image: --]
José Antonio Antillón
[image: http://]about.me/aantillon
<http://about.me/aantillon?promo=email_sig>
…On Mon, Jul 17, 2017 at 12:08 AM, Mrudul Tarwatkar ***@***.*** > wrote:
@ajahongir <https://github.com/ajahongir> Nope. That doesn't work. I
didn't override to_json method. Though, as I'm using rails-api with the
following gems:
gem 'oj'
gem 'oj_mimic_json'
gem 'jbuilder'
My guess is that, this has something to do with views (view_context) as
it works fine with normal non-api based projects.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#240 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAUXZ3gu9vZyCKA2c7re-y3GRqCL1SNhks5sOwh2gaJpZM4ONsFN>
.
|
@mrudult I might be wrong, but isn't it about passing record ID to Datatables? I'm using Datatables Editor and in my Index view I'm populating HTML only for opening Index page, then later JSON is used for CRUD. For Editor, here is info, how you customize DT_RowId. |
@matissg Not sure. As I said, my configuration works fine in a non-api based project without passing the Any help will be highly appreciated. |
|
The solution : In Rails.application.routes.draw do
resources :posts do
post 'datatable', on: :collection
end
end In controller : def datatable
render json: PostsDatatable.new(params)
end In datatable : class PostsDatatable < AjaxDatatablesRails::Base
# the trick starts here
attr_reader :params
def initialize(params, options = {})
@params = params # this is what we're looking for, so don't rename it
@options = options
load_orm_extension
end
# the trick ends here
def view_columns
@view_columns ||= {
title: { source: 'Post.title' },
content: { source: 'Post.content' },
}
end
def data
records.map do |record|
{
title: record.title,
content: record.content,
'DT_RowId' => record.id,
}
end
end
def get_raw_records
Post.all
end
end The test : #!/usr/bin/env ruby
require 'json'
require 'yaml'
require 'rest-client'
params =
{
'draw' => '1',
'columns' => {
'0' => {
'data' => 'title', 'name' => '', 'searchable' => 'true', 'orderable' => 'true',
'search' => {
'value' => '', 'regex' => 'false'
}
},
'1' => {
'data' => 'content', 'name' => '', 'searchable' => 'true', 'orderable' => 'true',
'search' => {
'value' => '', 'regex' => 'false'
}
},
},
'order' => {
'0' => {'column' => '0', 'dir' => 'asc'}
},
'start' => '0', 'length' => '10', 'search' => {
'value' => '', 'regex' => 'false'
},
'_' => '1423364387185'
}
response = RestClient.post('http://localhost:3000/posts/datatable.json', params)
json = JSON.load(response.body)
puts YAML::dump(json) The result :
|
@n-rodriguez Nah. Still the same output. I don't get the I'm gonna try and reproduce this tomorrow. Thanks |
After digging deeper, the oj_mimic_json gem was the culprit. I guess, it was calling the However, I'm getting the same issue as this when upgrading to Rails - 4.2.8, rails-api (0.4.1), ajax-datatables-rails (0.4.0) |
I'm trying to use this in API only mode with Rails - 4.2.8, rails-api (0.4.1), ajax-datatables-rails (0.4.0)
When I try to hit the url via curl it only prints the datatable's object. Here's the datatable class:
The controller
When I hit the url
/app/users.json
it just gives"#<UserDatatable:0x007f92b89a4d70>"
as output.I tried this in a non-api project (by directly doing a curl on the url) and it works fine (even in the html.erb templates everything works) with an output like:
What am I possibly doing wrong here? I'm essentially trying to use this gem for server side processing and using JQuery Datatable with AngularJs as front-end.
The text was updated successfully, but these errors were encountered: