2

so I have the following

# app/helpers/experts_helper.rb

module ExpertsHelper
....
def twitter_link(expert)
  link_to expert.twitter_url, class: 'social-link', target: '_blank' do
   content_tag(:i, '', class: 'fa fa-twitter expert-fa')
  end
end

def linkedin_link(expert)
  link_to expert.linkedin_url, class: 'social-link', target: '_blank' do
    content_tag(:i, '', class: 'fa fa-linkedin expert-fa')
   end
end
end

I can access these methods in all my views inside views/experts/... but not in those inside views/admin/experts/... How can I access these them? Thanks

EDIT 1:: Corrected some typos, also I want to be able to use these helper methods on class Admin::ExpertsController < AdminController (content omitted for brevity) end I have used include ExpertsHelper inside the controller, but it doesn't solve it

EDIT 2: I have tried all proposed solutions but still don't get anything to be displayed.

I have tried the include ExpertsHelper and the include ApplicationController.helpers.method, with the latter I get TypeError - wrong argument type Array (expected Module): and with the former I don't get any errors, but I don't see anything being displayed on the page, no icons, no photos, nothing. I even tried copying the helper methods inside module Experts inside the module Experts::AdminHelper end, but still nothing.

intercoder
  • 2,171
  • 7
  • 23
  • 34
  • Do the `admin/experts/...` views use a different controller? I imagine you may just need to `include ExpertsHelper` in that controller if you aren't including all helpers by default. – TJR Mar 09 '16 at 15:38
  • 1
    Well for starters your module should be named `ExpertsHelper` not `Experts Helper` - the latter actually declares `Experts::Helper`. – max Mar 09 '16 at 15:38
  • @Max, just fixed the typo – intercoder Mar 09 '16 at 17:27
  • @TJR, what I have done so far 1. Added `config.action_controller.include_all_helpers = true` to `application.rb` 2. Did a manual `include ExpertsHelper` inside `Admin::ExpertsController < AdminController` I still cannot see anything – intercoder Mar 09 '16 at 17:31
  • Using `include ExpertsHelper` inside your class will include the methods defined in the `ExpertsHelper` module. This is pure ruby and really has nothing to with rails. I'm guessing that the error lies elsewhere or that you might just be trying to something weird like calling your method outside of render. It might help if you can add the relevant parts of your `Admin::ExpertsController` - what you have tried to do to verify that you class has the method and the exact error message. "When I tried to do A, I got error message B". http://stackoverflow.com/help/mcve – max Mar 09 '16 at 17:37
  • @max. I have tried the `include ExpertsHelper` and the `include ApplicationController.helpers.method`, with the latter I get `TypeError - wrong argument type Array (expected Module):` and with the former I don't get any errors, but I don't see anything being displayed on the page, no icons no photos, nothing. I even tried copying the helper methods inside `module Experts` inside the `module Experts::AdminHelper end`, but still nothing – intercoder Mar 10 '16 at 09:07

2 Answers2

1

I hope you are looking for this. Add this line in the controller where you want to access helper methods.

include ApplicationHelper
Ahsan Ellahi
  • 336
  • 2
  • 6
  • with `include ApplicationController.helpers.method`, I get `TypeError - wrong argument type Array (expected Module):` – intercoder Mar 10 '16 at 09:08
0

I believe you can use ActionView::Rendering#view_context to use those helper methods. In your controller you would use them like so:

view_context.twitter_link(expert)
view_context.linkedin_link(expert)

See this other SO question for more details and options.

Community
  • 1
  • 1
Jack Collins
  • 1,145
  • 10
  • 21