3

I'm trying to access a helper variable from a haml view in my project. I have the helper defined in rails-root/app/helpers/table_field_helper.rb:

module TableFieldHelper
    def table_display_fields
        MORE_COMPLICATED_TABLE_DISPLAY_FIELDS #some array defined elsewhere in the file
    end
    ...
end

And trying to iterate over table_display_fields in index.html.haml:

...
%tr
    %th
    - table_display_fields.each do |field|
        %th= field
...

I'm getting an error: undefined local variable or method 'table_display_fields'

What's causing this error? Why am I unable to access the definition in TableFieldHelper? I am new to Ruby/Rails, but I was under the assumption that all helpers are implicitly included in view/layout files, and nothing I've read would suggest I have to do anything extra to make them work together. Thanks!

Nathan Brown
  • 311
  • 1
  • 3
  • 12
  • 2
    Depending on your Rails version it might be that controller only includes helper file with matching name as explained here http://api.rubyonrails.org/classes/ActionController/Helpers.html – Mike Szyndel Jan 22 '16 at 17:22
  • Would this affect the views as well though? I don't see anything directly pertaining to views, just controllers. Edit: I'm using jrails with rails 4.0.9, so I don't think that's the problem based on what's written http://stackoverflow.com/questions/1179865/why-are-all-rails-helpers-available-to-all-views-all-the-time-is-there-a-way-t – Nathan Brown Jan 22 '16 at 17:26
  • A view only has access to what controller made available – Mike Szyndel Jan 22 '16 at 17:27
  • `include TableFieldHelper` in controller – Andrey Deineko Jan 22 '16 at 18:12
  • I'd tried that before @AndreyDeineko , gave me `uninitialized constant IndexController::TableFieldHelper`. Is it necessary to include though? Most of the code I've been using as examples doesn't have include lines in the controllers. – Nathan Brown Jan 22 '16 at 18:18
  • 1
    `include ::TableFieldHelper`, As @Michal Szyndel said, only modules with the name that matches controller's name are automatically included – Andrey Deineko Jan 22 '16 at 18:19
  • So, despite my rails version seeming to be high enough to not require like-named helpers, renaming the helper fixed the problem. – Nathan Brown Jan 25 '16 at 19:45

2 Answers2

1

To use mixin methods from module you have to include it.

With controllers (unlike with models) the difference is that the module will be automatically included, if it has the name that matches corresponding controller.

In your case adding the following to controller will fix the issue:

include ::TableFieldHelper
Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145
  • Still no luck with this, and it gives me the same error when I include it in `index_controller.rb`, even if I put it in `application_controller.rb` – Nathan Brown Jan 22 '16 at 19:14
  • I do not believe it throws the same `uninitialized constant IndexController::TableFieldHelper` error when you use `::TableFieldHelper`. Did you notice `::`? – Andrey Deineko Jan 22 '16 at 20:04
1

If the helper class name is not the same as the controller name then you will have to manually require the helper class. http://api.rubyonrails.org/classes/ActionController/Helpers.html You can either change the name of the helper class to the same name as the controller that you are using it in, or you can include it in the controller.

ruby_newbie
  • 3,190
  • 3
  • 18
  • 29