rails 4.1 helpers

helpers are methods inside your app/helpers folder, and they are available to your views usage. Every controller gets one (if the controller gets created via a generator).

Example of an application helper:

module ApplicationHelper
  def title(*parts)
    unless parts.empty?
      content_for :title do
        (parts << "Ticketee").join(" - ")
      end
    end
  end
end

The * means any argument passed will be available as an array

This particular helper can be called via this example code (in your application.html.erb template):

<title>
  <% if content_for?(:title) %>
    <%= yield(:title) %>
  <% else %>
    Ticketee
  <% end %>
</title>

And, the particular pages or sub-templates that call this:

<% title(@project.name, "Projects") %>

Leave a Reply

Your email address will not be published. Required fields are marked *