0

I tried to create a helper module to be able to set the title of a page. Of course it's not working (reference) Is there something I must define in a controller for my helpers methods to be seen by my controllers??

Undefined method

Gitlink: works_controller.rb

  def index
    set_title("Morning Harwood")
    @works = Work.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @works}
    end
  end

In application_helper.rb:

module ApplicationHelper
    def set_title(title = "Default title")
      content_for :title, title
    end  
end

In the layout work.html.erb:

 <%= content_for?(:title) ? content_for(:title) : 'This is a default title' %>
Community
  • 1
  • 1
Armeen Moon
  • 18,061
  • 35
  • 120
  • 233

1 Answers1

3

Helpers in Rails are methods available in views (and controllers if you include them) that allow you to avoid code repetition in views.

An example of a helper from my code is a method that renders html for facebook login button. This button is in reality more than user sees, because it's a hidden form with some additional information, etc. For this reason I wanted to make a helper method out of it, so instead of copying 10 lines of code multiple times I can call a single method. This is more DRY.

Now, back to your example, you want to do two things

  • display page <title>,
  • add <h1> header at the top of the page.

I see now where linked answer wasn't clear enough. You indeed need helper, but you also need to call it! So

# application_helper.rb
def set_title(title = "Default title")
  content_for :title, title
end

# some_controller.rb
helper :application

def index
  set_title("Morning Harwood")
end

And then in layout's views you can use:

<title> <%= content_for?(:title) ? content_for(:title) : 'This is a default title' %><</title>
...
<h1><%= content_for?(:title) ? content_for(:title) : 'This is a default title' %></h1>
Mike Szyndel
  • 10,461
  • 10
  • 47
  • 63
  • how would I make this into a helper class? – Armeen Moon Jul 21 '13 at 08:26
  • But why do you insist on having a helper, do you understand what helpers are for? – Mike Szyndel Jul 21 '13 at 08:27
  • 1
    And helpers are not CLASSES, they are MODULES. – Mike Szyndel Jul 21 '13 at 08:27
  • Sorry for my lack of understanding of the rails and its proper nomenclature. I want to create a title helper because the linked referenced above stated that it was best practice to do so. I felt that this was a good starting point in understanding helper MODULES. I feel understanding how to create helpers will be beneficial for my next task; which is to, create a helper that allow me to add a current class to a anchor tag from a current route. http://stackoverflow.com/questions/3705898/best-way-to-add-current-class-to-nav-in-rails-3 – Armeen Moon Jul 21 '13 at 08:39
  • @MatthewHarwood check out my more in-depth explanation. – Mike Szyndel Jul 21 '13 at 08:41
  • Hey michael... I'm sorry thanks for all your help. I'm getting a undefined method `content_for' ... I placed the content_for(:title, "Morning Harwood") inside my works_controller.rb – Armeen Moon Jul 21 '13 at 08:46
  • Ah, okay, this is one of stupid limitations of Rails. Rewriting my answer – Mike Szyndel Jul 21 '13 at 09:01
  • Damn yet again, undefined method `set_title' for # Updated code with full blocks. I feel stupid, is there something in this code that's erroring out your answer? – Armeen Moon Jul 21 '13 at 10:11
  • Is there anyway the helpers arn't being linked(?)? All my models, controllers, and views were generated from Rails' scaffolding. I didn't explicit do anything to link(?) the helpers. – Armeen Moon Jul 21 '13 at 10:18
  • What do you mean by link? – Mike Szyndel Jul 21 '13 at 10:34
  • Like maybe the helpers arn't even active( link like not being seen because they are dependant on another file)? I don't know... Just throwing out ideas... Been trying to read documentation... I have no idea why I'm getting this no method error – Armeen Moon Jul 21 '13 at 10:36
  • Stupid me, updating again! You need to add `helper :application` in controller – Mike Szyndel Jul 21 '13 at 10:37
  • I'm in bed now but I'll test it out first thing in the morning? and mark it right. Lol or follow up with more errors thanks so much for your help – Armeen Moon Jul 21 '13 at 10:39
  • I hate to keep coming back here but yet again no method error is being thrown... I've updated my post with a very thorough visual of my process. Do I have to do something with route to make a helpper work? I know you're just as curious as I am as to why this still isn't working... I promise you, I will know wtf a module is after this. – Armeen Moon Jul 21 '13 at 23:17
  • @MatthewHarwood there's a similar, working, solution here: http://stackoverflow.com/questions/3059704/rails-3-ideal-way-to-set-title-of-pages/3092076#3092076 – Andrea Pavoni Mar 24 '14 at 14:57