Bharat
Bharat

Reputation: 2469

problems upgrading from rails 3.2.16 to Rails 4.0.2 -- NoMethodError - private method `h' called for

I am trying to upgrade from a Rails 3.2.16 application to Rails 4.0.2 and am running into the following problem.

I have a class called BaseDataTable shown in the following code snippet:

class BaseDatatable
  delegate  :h, :link_to, to: :@view
  ...
end

Then I call the h method delegated above (in addition to link_to) from the classes that inherit from BaseDatatable like so:

class TypeWellsDatatable < BaseDatatable

  private

  def row(type_well)
    ret_array = []
    ...
    ret_array << h(phases)
    ...
  end
  ...
end

This works fine in Rails 3.2.16, but throws the following error in Rails 4.0.2:

NoMethodError - private method `h' called for #<#<Class:0x00000107c8d2c0>:0x00000100ed8310>:
  app/datatables/base_datatable.rb:2:in `h'
  app/datatables/type_wells_datatable.rb:22:in `row'
  app/datatables/base_datatable.rb:13:in `block in as_json'
  activerecord (4.0.2) lib/active_record/relation/delegation.rb:13:in `map'
  app/datatables/base_datatable.rb:13:in `as_json'
  activesupport (4.0.2) lib/active_support/json/encoding.rb:50:in `block in encode'
  activesupport (4.0.2) lib/active_support/json/encoding.rb:81:in `check_for_circular_references'
  activesupport (4.0.2) lib/active_support/json/encoding.rb:49:in `encode'

It seems like the ActiveSupport gem has made this method private in Rails 4.0.2 which makes no sense to me (or I just do not understand). My question is: what is the least invasive way of getting around this issue? There are a number of other classes that inherit from BaseDatatble class and use the same technique.

Upvotes: 1

Views: 1000

Answers (1)

samsaradog
samsaradog

Reputation: 96

h is of course an alias for html_escape. Starting with Rails 3, all strings were html escaped by default. The Rails 4 documentation still shows html_escape as a public method on ActiveView::Base (actually ERB::Util) but in the 4.0.2 version of Rails I'm using, view_context is returning false to respond_to?.

It would not surprise me that Rails 4 no longer lets you call html_escape explicitly. This is consistent with the move to whitelisting, meaning that you have to declare strings safe so that they aren't escaped. Otherwise it's assumed they're unsafe.

Net is you can just get rid of your calls to h and use the bare string.

Upvotes: 8

Related Questions