Friday, 18 April 2014

Adding title to Rails view

You can add your title for views in two ways.for adding title in views you have to follow these steps:

1-> add <title><%= yield(:title) %></title> inside the header of your layout.
2-> now you can add your title inside your view.there are following ways to do that:

  a-> use this line in your view:
      <%= content_for(:title, "Title for specific page") %>

  b-> use helper method:
  •  add a helper method in application_helper

       def title(page_title)
            content_for :title, page_title.to_s
      end


  • inside your view:
           <%= title "your custom title" %>
  c-> if you want to write title for view in controller then add this method inside   your application_controller.

  def view_context
    super.tap do |view|
      view.content_for :title, "your title"
    end
  end


if you want to add title according to your controller and action names pair then:

  def view_context
    super.tap do |view|
      view.content_for :title, "#{controller_name}_#{action_name}"
    end
  end

 

No comments:

Post a Comment