memoht
memoht

Reputation: 781

Adjust Title Helper in Ruby on Rails Tutorial 3.2 to deal with & properly

I am using the title helper from the 3.2 edition of the Ruby on Rails Tutorial by Michael Hartl and just realized a snag with the & character showing up in the title as &Amp; instead.

The relevant snippet of code is here Official Sample App 2nd Edition


The problem. I have a School model and am using the School name on the Show view as follows:

<% provide(:title, @school.name) %>

If my School has a & in the name, it is being replaced with &Amp; in the browser title.


Ryan Bates Railscasts site has a similiar title helper that solves this issue this way but it is using content_for instead of provide.


Trying to adjust the Rails Tutorial helper, but having trouble getting it work properly. Works great expect for this issue.

Upvotes: 0

Views: 160

Answers (1)

Suhail Patel
Suhail Patel

Reputation: 13694

You can modify the page_title helper and add html_safe to the output strings so does not convert the ampersands and leaves them intact

def full_title(page_title)
  base_title = "Ruby on Rails Tutorial Sample App"
  if page_title.empty?
    base_title.html_safe
  else
    "#{base_title} | #{page_title}".html_safe
  end
end

Upvotes: 1

Related Questions