ajax-datatables-rails: undefined method `to_unsafe_h' for nil:NilClass
I am getting Error 1 when trying to load the datatable view. When I visit the .json page at ‘prices/index.json’ I see this:
NoMethodError in PricesController#index undefined method `to_unsafe_h’ for nil:NilClass
respond_to do |format|
format.html
format.json { render json: PricesDatatable.new(view_context) }
end
end
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 4
- Comments: 20 (1 by maintainers)
put it in the controller before calling Datatable
It’s 2019 and I still see this problem. Is there any real fix for it?
problem exist for us as well. any solution except downgrading?
@mrudult I think this issue is coming when we have slow internet speed or loading issue. otherwise it is working fine.
The error occurred in
get_parammethod. Our params that is first argument must be aActionController::Parametersinstance. Because theto_unsafe_his a method of ActionController::Parameters. When you change the params to a hash you have the error.I have same problem here. I don’t understand where I should build these definitions to be passed, and foremost why? This is just wierd and wrong imho.
It’s because prices/index.json doesn’t have the column definition in the URL. The options hash contains the params from the URL and as such needs all of the column information. It didn’t used to be this way, you used to be able to call the .json on the URL and just get the raw json, but now to do that you have to add the column defs to the URL.
You can manually build it if you need to, note that the URL is available in the js console of the page (not for json only obviously, but for your regular datatables page request).
The pieces of data that are necessary are:
The index of the columns array has to match the index of your data records array (or hash), as does the data attribute. Once you’ve populated the data for ALL the columns you want, open the console and run your string through URI to encode it.
You’ll get something like this:
Add the encoded string to your URL (domain + path) and the json should load without error.
@adamw005 Had the same issue. Downgrading to 0.3.1 worked for me.
Basically the change is here