Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: How to include meta tags in views and not in application.html.erb?

I want to include meta tags in my homepage view. But I do not want to include it in the head section of the application.html.erb file directly, as this will cause it to include the meta tags on all pages which is redundant for my app.

like image 374
pratski Avatar asked Jun 26 '13 12:06

pratski


People also ask

How do I view HTML ERB?

View templates HTML tags provide static web pages only but ERB tags give us dynamic information in that HTML template. To view the template file, go to Rails application >> app >> View>> Home folder where the templates files are available.

What is Csrf_meta_tags?

csrf_meta_tags are indications for ajax requests to use these as one of the form parameters to make a request to the server. Rails expects the csrf as part of your form body (params) to process your requests. Using these meta tags you can construct the form body or the csrf header to suit your needs.

Where is meta tag only found?

<meta> tags always go inside the <head> element, and are typically used to specify character set, page description, keywords, author of the document, and viewport settings. Metadata will not be displayed on the page, but is machine parsable.


1 Answers

You can put

<% if content_for?(:head) %>
  <%= yield(:head) %>
<% end %>

in head section of application.html.erb layout.

Then, in view in which you want to include these tags:

<% content_for :head do %>
  <!-- your tags -->
<% end %>
like image 159
Marek Lipka Avatar answered Oct 19 '22 15:10

Marek Lipka