1

Each page of my site has a unique title. I'm repeating myself and adding code to multiple pages when I feel I can do this with a layout and helper. I'll show my working code and the solution I'm developing which is failing and I need some help on

Page titles are basically of this form:

<title>Company Name - Page Description</title>

Right now my layout does this:

<%= content_for?(:page_title) ? yield(:page_title) : default_page_title %>

I have a application helper method like this:

def default_page_title
    content_tag(:title, 'My Company Name - the default description here')   
end

then each page has something like this:

<% content_for(:page_title) do %>
    <%= content_tag(:title,'My Company Name - Page specific description here') %>
<% end %>

It works but I do not want to add these snippets to the top of every page... I also do not want to repeat "My Company Name - " on each page. I want to develop a solution where the page descriptions are all filled out in an application helper and the application layout selects the right one based on controller and action.

I'm working on a helper like below (failing for various reasons I hope someone can help me with):

def page_title
  content_tag(:title, "My Company Name - " + page_description )

  def page_description

    case params[:controller].params[:action]
    while public.index
    'Description for public controller index action'
    while public.contact
    'Description for public controller contact action'
   end
  end
end

This will allow me to make page specific changes to title in the application_helper instead of modifying each page.

4

1 回答 1

1

我不会推荐这个..我在一些项目中自己做了,但一段时间后最终得到了视图中指定的各个标题。如果您有一些更复杂的表单/工作流程,则不容易识别路径以及相关的操作...

于 2013-01-21T16:27:23.440 回答