Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apostrophe Appearing as "'" in Title Tab of Rails App

Apostrophes are appearing as "'" in the title tabs of my Rails application. Everywhere else the apostrophes appear correctly. How may I correct this issue?

Thank you.

like image 743
Richard74 Avatar asked Oct 15 '25 08:10

Richard74


2 Answers

I'm not sure whether you want the apostrophe to appear as ' or as '.

If you put your apostrophe in double quoted strings then it will render correctly:

<title><%="example 'title'" %></title> #=> example 'title'

If you put &#39; inside double quoted strings then it will be returned as it is written.

<title><%="example &#39;title&#39;" %></title> #=> example &#39;title&#39;

Finally, if you want to use &#39; and have it parsed correctly then you can use raw.

<title><%=raw "example &#39;title&#39;" %></title> #=> example 'title'

If it is not working as expected then it may be a browser issue, however, I have tested with modern versions of Firefox and Safari and it is working correctly.

like image 189
Tom Kadwill Avatar answered Oct 17 '25 01:10

Tom Kadwill


To help those with their "Google Fu".

If you are here because you are doing the rails title on every page like SO: rails-how-to-change-the-title-of-a-page and have records where your apostrophe is appearing with the &#39 and simply want it with an ' (ie My Dog's Pants instead of My Dog&#39s Pants) then you can checkout this answer that uses ruby string interpolation #{} and .html_safe.

Passing string with apostrophe to helper method doesn't display correctly

As an example, I put a method in my post.rb model:

  def page_title
    "#{ id.to_s +  " - " + name }".html_safe
  end

Then in my posts_controller.rb I have:

@title = @post.page_title

Then in my application.html.erb I have:

<body>
  <title><%= ("Post - " + yield(:title) + " - " unless yield(:title).blank?).to_s + "jaykilleen.com" %></title>
</body>

Then in my show.html.erb for posts I have:

<% content_for :title, @title if @title.present? %>
like image 31
Jay Killeen Avatar answered Oct 17 '25 01:10

Jay Killeen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!