thurmc
thurmc

Reputation: 525

Link tag printing rather than being interpreted

I am trying to include a css file in a layout file on a rails site and my tag contained in the header is not being interpreted, it is instead displaying on the page. I put the tag in the section. The link tag is shown below. I assign page_style and it is correctly inserted on the page but the page displays the second tag below instead of interpreting the tag.

<%= "<link rel=\"stylesheet\" type=\"text/css\" href=\"/stylesheets/#{@page_style}.css\">"    if !@page_style.nil? %>

<link rel="stylesheet" type="text/css" href="/stylesheets/standard.css">

Upvotes: 0

Views: 28

Answers (2)

Unixmonkey
Unixmonkey

Reputation: 18784

In Rails (3 and up), strings are HTML-escaped before being shown, so your < are being encoded as &lt;, etc.

You can add .html_safe to your string like this:

<%= "<link rel=\"stylesheet\" type=\"text/css\" href=\"/stylesheets/#{@page_style}.css\">".html_safe if !@page_style.nil? %>

However, it would be a bit more Rails-y if you rewrote it to use the stylesheet_link_tag helper like this:

<%= stylesheet_link_tag(@page_style) if @page_style.present? %>

Upvotes: 1

Philipp Meissner
Philipp Meissner

Reputation: 5482

I don#t really see where you started the Rails-command. Usually basic Rails starts the following:

<% if @page_style.nil? %>
    // Do something here
<% end %>

You may want to add 'binding.pry' within your controller right after @page_style was used. Within your server-tab it then should open up your rails console where you can check the output of @page_style (and even apply the @page_style.nil? operation and check its outcome).

Upvotes: 0

Related Questions