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)

Most upvoted comments

undefined method `to_unsafe_h’ for nil:NilClass problem exist for us as well. any solution except downgrading?

put it in the controller before calling Datatable

params["columns"] ||= { "0" => {"data" => "" } }
params["length"]  ||= -1

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_param method. Our params that is first argument must be a ActionController::Parameters instance. Because the to_unsafe_h is a method of ActionController::Parameters. When you change the params to a hash you have the error.

File: vendor/bundle/gems/ajax-datatables-rails-1.0.0/lib/ajax-datatables-rails/datatable/datatable.rb
70:       def get_param(param)
71:         options[param].to_unsafe_h.with_indifferent_access
72:       end

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:

columns[0][data]=id&
columns[0][name]=&
columns[0][searchable]=true&
columns[0][orderable]=true&
columns[0][search][value]=&
columns[0][search][regex]=false&

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.

2.3.4 (main):0 > URI.encode("columns[0][data]=id&
2.3.4 (main):0 * columns[0][name]=&
2.3.4 (main):0 * columns[0][searchable]=true&
2.3.4 (main):0 * columns[0][orderable]=true&
2.3.4 (main):0 * columns[0][search][value]=&
2.3.4 (main):0 * columns[0][search][regex]=false&
2.3.4 (main):0 * ")

You’ll get something like this:

"columns[0][data]=id&%0Acolumns[0][name]=&%0Acolumns[0][searchable]=true&%0Acolumns[0][orderable]=true&%0Acolumns[0][search][value]=&%0Acolumns[0][search][regex]=false&%0A"

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