heroxav
heroxav

Reputation: 1467

Rails: How to put stuff in header

In my Rails App I included OneSignal which requires the following link to be placed in the head of the document:

<link href='/manifest.json' rel='manifest'>

Unfortunately with Rails I cannot put this link there, because the entire layout gets rendered inside the body.

While this ...

$(document).ready(function(){
    $("head").append("<link href='/manifest.json' rel='manifest'>");
});

seems to work in development mode, it does not work in production.

How can I add this link to my head section of the document?

Upvotes: 2

Views: 2349

Answers (2)

Jeremy
Jeremy

Reputation: 675

If you don't want to make a site wide change (adding the script to every page on the site) you can pick the page you want to put it on and add to the .html.erb file

<% content_for :header do %>
    <%= javascript_include_tag "script.js" %>
<% end %>

javascript_include_tag will allow you to send content from the controller to the header and if script.js is in your app/assets/javascript directory, that will be added to the header.

Another option is to make a specific layout and have the pages that need the script use that layout, by calling it in the controller like so:

def users_index
@user= User.find(params[:id])
layout: users_index layout 
end

That will load the users_index_layout.html.erb file from app/views/layouts...if it exists, and complain loudly if it does not.

Upvotes: 4

user7330717
user7330717

Reputation:

You can add <link href='/manifest.json' rel='manifest'> in app/views/layouts/application.html.erb.

Upvotes: 0

Related Questions