Wanting same banner/nav menu on each page in Rails - html

I'm new to rails. I currently have 3 pages in my web app. I am wondering what is the "cool rails way" where I can easily implement the same banner (pic/website logo/text) and horizontal navigation menu on each page with out having to copy and paste the code or create separate CSS files for each page. Is this possible? Would like to stick with Ruby/Rails/CSS/HTML.
Also, It seems that if I create a css element titled banner for one view of one controller another view of a different controller has the same styling. Is there something going on behind the scenes here?
Thanks.

This is usually accomplished through application.html.erb. You put the header of your web site in that page followed by a <%= yield %> statement and then the footer. The yield statement inserts the content from whatever action is being processed.
For example:
application.html.erb
<html>
<head>
</head>
<body>
<h1>My cool web site header and menu!</h1>
<%=yield %>
<p>Web site footer</p>
index.html.erb
<h2>Index</h2>
<ul>
<% #stuff.each do |thing| %>
<li><%=thing.name%></li>
<% end %>
</ul>
The contents of index.html.erb will be inserted into the application.html.erb at the spot where <%=yield %> appears.

Following this tutorial: http://ruby.railstutorial.org/ruby-on-rails-tutorial-book ,
the way to do it is to use rendermethod.
example code from the tutorial:
app/views/layouts/application.html.erb
<!DOCTYPE html>
<html>
<head>
<title><%= full_title(yield(:title)) %></title>
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" =>
true %>
<%= javascript_include_tag "application", "data-turbolinks-track" =>
true %>
<%= csrf_meta_tags %>
<%= render 'layouts/shim' %>
</head>
<body>
<%= render 'layouts/header' %>
<div class="container">
<%= render 'layouts/flash' %>
<%= yield %>
<%= render 'layouts/footer' %>
</div>
</body>
</html>
According to the tutorial, the way in rails to pack up a logical unit in one place is to use a facility called partials.
For example, you can pack up your navigation bar in app/views/layouts/_navbar.html.erb
then it can be rendered in the layout with <%= render 'layouts/navbar' %>
More about the parials: http://ruby.railstutorial.org/chapters/filling-in-the-layout#sec-partials
Rails defaults to concatenating all CSS files into one master. http://guides.rubyonrails.org/asset_pipeline.html

Related

failing to render application.html.erb

I am failing to render my application.html.erb template i am getting the following error
ActionView::SyntaxErrorInTemplate in ApplicationController#home
app/views/layouts/application.HTML.erb:10: syntax error, unexpected ':', expecting ')'
app/views/layouts/application.HTML.erb:11: syntax error, unexpected ':', expecting ')'
This is what the code looks like this application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>Appz</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag "application" , "data-turbo-track" : "reload" %>
<%= javascript_include_tag "application" , "data-turbo-track" : "reload" , defer: true %>
</head>
<body>
<div class="container">
<div class="row">
<div class="col">
</div>
</div>
</div>
<%= yield %>
</body>
</html>
my code looks ok I have tried everything what could be the problem
You need to remove white space before :. Change the line and below line like this:
<%= stylesheet_link_tag "application" , "data-turbo-track": "reload" %>
Yes removing the white space works apparently I had a formatting extension enabled in vs code and it was messing up my code it was auto indenting unnecessarily and messing up the code I only became aware when I tried to correct the code by removing the white space and each time I saved it auto indented until I disabled the extension and it stopped I have since uninstalled the extension.
So be careful what kind of extensions you install in your code editor you will get new and unfounded errors no one has seen before.

Cannot include css in application in rails

I'm using rails 4
and ruby 2.1.5
And I generate controllers rails generate .scss file for me I write specific code in css but it doesn't gets include in application?
I link it with
<%= stylesheet_link_tag 'name of css' %>
and I do this in html.erb file in views
What should I do ?
If this is silly sorry I'm new to rails..
You need to call the CSS in the layout:
#app/views/layouts/application.css
<%= stylesheet_link_tag "name_of_css" %>
This will call the CSS in the <head> tag of your HTML page.
If you want to call css conditionally, you'll have to set it in the stylesheet_link_tag declaration:
<%= stylesheet_link_tag :application, ("tester" if controller_name == "tests") %>

Is Rails going "backwards" in the code in this example?

I have following html page home.html.erb:
<% provide(:title, 'Home') %>
<h1>Sample App</h1>
<p>
This is the home page for the
Ruby on Rails Tutorial
sample application.
</p>
And I have the following layout application.html.erb
<!DOCTYPE html>
<html>
<head>
<title><%= full_title(yield(:title)) %></title>
...
</head>
<body>
<%= yield %>
</body>
</html>
full_title() is a function that checks if there was a title passed as a parameter. If there was, it will place it into the HTML. If no parameter is given, it will place a base title into the HTML.
I'm assuming rails begins by going through the application.html.erb and then upon reaching <%= yield %>, it will embed the contents of home.html.erb into application.html.erb at that location, resulting in the following document:
<!DOCTYPE html>
<html>
<head>
<title><%= full_title(yield(:title)) %></title>
...
</head>
<body>
<% provide(:title, 'Home') %>
<h1>Sample App</h1>
<p>
This is the home page for the
Ruby on Rails Tutorial
sample application.
</p>
</body>
</html>
In the 4th line <%= full_title(yield(:title)) %>, the value "Home" is definitely being passed for :title, but the code <% provide(:title, 'Home') %> doesn't come until several lines later. Is Rails going backwards to accomplish this? How is this happening?
No it's don't. This is kind of string interpolation, this value will be replaced after every call of provide(:title, ...)
My understanding is that the home.html.erb is processed before the application layout. In other words, the view and helper are prepared and 'plugged-in' to the application layout before the whole thing is complete. I guess "going backwards" depends on which way you look at it / how it actually works. I'm still learning too, but it's not necessarily a 'top to bottom' sequence in terms of how the code is layed out on the page.

Rails reading in and rendering seperate html doc, need <base> to only apply to the read in html

I am writing a mobile web application using Rails and jQuery mobile. I am reading all of my data at runtime from Amazon AWS S3 using HTTParty.
One of the screens that I need to render is just straight html, which can and usually does have images embedded into it which are hosted in the same folder on S3. This is where my problem is. I can easily pull in the html with HTTParty and use the html_safe option to render it, but the images don't render as they are relative path names. So I have been trying to find a way around this.
I have tried multiple things, but I have mainly been looking into using an html tag to get the images to point to the right location. The problem is that I cant specify a base tag and then have other links on the page, because they then use that same base and the links are not pointing atand the correct location. So I looked into framesets and frame and pointing the base tag only at the frame, which I believe I used correctly, but to no avail. I tried using but to no avail.
So basically I am looking for a way that I can set the base for relative path names in an html string that I read in from S3, if that wasn't clear. I am open to any suggestions! And thanks in advance for even reading and try to solve this very specific problem!
Oh and one more thing, when I look at the page with Firebug, the first line in the header is a base tag with href set to the current page. I can't find out where it is coming from but I am guessing rails is throwing it in there? I don't know if this matters since I then put another base tag below it with the yield :intro_base? Or is that one of my problems since there is a conflict there?
And then there was code:
My 'intro' method:
def intro
#intros = []
#app_config.intro_screens.each do |intro_screen|
intro_screen_response =
ApplicationController.get("#{#diandr_url}/#{intro_screen['filename']}.html")
#intros << intro_screen_response.body
end
#intros.reverse!
#intros_length = #intros.length
respond_to do |format|
format.html
end
end
My 'intro.html.erb' file:
<% page_num = params[:id].to_i %>
<% content_for :intro_base do %>
<base href="https://s3.amazonaws.com/our_bucket_name<%=#dir_url%>/" target="intro" />
<% end %>
<% content_for :mobile_header do %>
*some jQuery mobile paging and header stuff is in here, shouldn't matter*
<% end %>
<% content_for :mobile_content do %>
<!-- <iframe src=<%= #intros[page_num] %> height="100%" width="100%"> -->
<!-- <p> This browser does not support iframes </p> -->
<!-- </iframe> -->
<frameset cols="100%">
<frame name="intro" <%= #intros[page_num].html_safe %>
</frameset>
<% end %>
My layout's header:
<head>
<title> our Mobile App </title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- These are the jQuery Mobile scripts -->
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.js">
</script>
<%= yield :intro_base %>
<%= stylesheet_link_tag "master" %>
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>

What happens with Rails indentation in outputted HTML?

I'm writing code for a Rails view in TextMate (using the 2-space indentation standard). Whenever I view the output of my webpages (View Source), the HTML brackets always seem weirdly indented. For example, my application.html.erb looks like this:
<!DOCTYPE html>
<html>
<head>
<title>Rainleader</title>
<%= stylesheet_link_tag :all %>
<%= javascript_include_tag :all %>
<%= csrf_meta_tag %>
</head>
<body>
<div id="outer">
<div class="contentwidth main">
<%= render 'layouts/header' %>
</div>
<%= yield %>
</body>
</html>
And the partial it's rendering (_header.html.erb) looks like this:
<div class="logo">
<h1>minimal.</h1>
</div><!-- end logo -->
But, an excerpt of the outputted HTML has misplaced (mis-indented) brackets (see my notes in the code below):
<body>
<div id="outer">
<div class="contentwidth main">
<div class="logo"> <<<Why is this so far to the right?
<h1>minimal.</h1> <<<Why is this so far to the left?
</div><!-- end logo -->
What's going on here? If my call to the _header.html.erb partial in application.html.erb is indented four spaces, do I need to indent the code in the partial by at least that same amount to have it nest properly?
The first line of the partial that is rendered is indented as <%= render 'layouts/header' %> in application.html.erb. But all other lines of code are not indented further, just left-aligned as they are in your partial. It bugged me too, which is part of why I started using haml.