I am attempting to create an application.html.erb file that holds a background that will be displayed on all of my pages.
Using the w3school's guide, I put the filename of the background (which is in the same folder as application.html.erb) in the body tag and it does not change anything.
<body background='bk.jpg'>
The content goes here
</body>
Any help solving this issue would be greatly appreciated.
For a Rails project, the best approach is to use image_path.
In your case, something like the following should work:
<%= image_path 'path_to_your_image/bk.jpg' %>
However, the correct way to do this is to store the image in the projects app/assets/images folder, and the asset path will look for this there. Then, you just need to update the helper to:
<%= image_path 'bk.jpg' %>
So:
<body background=<%= image_path 'bk.jpg' %>>
# Might need quotes around this: <%= image_path 'bk.jpg' %>, haven't been able to test
Or, better:
<%= content_tag :body, style: { background: image_path('bk.jpg') } do %>
Your content
<% end %>
Better still would be to have the css background set in the CSS rather than inline, though that's perhaps another question.
Related
I'm new to Ruby on Rails and I'm trying to extract information I have from certain fields in a database and use them as means of styling.
For example, I have a height and width field in my 'Student' database. I wish to extract the height field content and width field content as parameters for my CSS file to set the height and width respectively of a div tag.
I am having a lot of trouble doing this. I have linked my stylesheet in the view index.html.erb by:
<%= stylesheet_link_tag 'students' %>
which is under assets/stylesheets/students.scss
I am not sure how to proceed.
If the styling is database driven, you should not rely on sprockets which generates static stylesheets during deployment.
A simple solution is to build css content using ERB.
<style>
.students-container {
height: "<%= #height.to_i %>px",
width: "<%= #width.to_i %>px"
}
</style>
You can extract out the style node into a partial and reuse it in multiple templates.
Seems like an inline style would work fine here. In your ERB, in your student divs, just do:
<% #students.each do |student| %>
<div style="height: <%= student.height %>px; width: <%= student.width %>px;">
<!-- other student stuff -->
</div>
<% end %>
It's either this or generating a unique CSS class for every student first, then using it in each div.
I have been trying to find something about embedding HTML code inside a partial argument for days but I have not found anything so I'm guessing it isn't possible. But it seems like it should be.
I have a static page in my Rails app which has a lot of sections and each section can have subsections. I could just make the entire page just plain HTML. But I didn't want to repeat the same formatting over and over in case I want to change classes or something else.
So I have the following _section.html.erb partial file:
<div class="row">
<h4><%= heading %></h4>
<% subsections.each do |section| %>
<% if section[:header] %>
<h5 class="primary-text"><%= section[:header] %></h5>
<% end %>
<p><%= section[:body] %>
<% end %>
</div>
That works fine. But what if I want to include a link to a page or an email inside one of the subsections? It doesn't work just by passing it in as part of the quotes text. It shows the actual HTML tags.
Is there a real way to do this or should I give up and just write plain HTML with repeated section formatting?
You mark your text as html_safe. For example:
<%= section[:header].html_safe %>
But I would suggest using sanitize method because of security resonons:
<%= sanitize section[:header] %>
Probably sometimes you will want to configure sanitize method. Here you can read how to do this:
http://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html
You can read more about security here:
http://guides.rubyonrails.org/security.html#cross-site-scripting-xss
I have a controller main_pages in a rails project that holds two views: home.html and team.html. All my css and styling for home and team has gone into main_pages.css.scss, but I would like to separate some of the styling that is specific to each of those pages into respective css stylesheets, such as home.css.scss and team.css.scss. Creating these stylesheets is not an issue, but how would I go about linking them?
I don't even know how home.html and team.html are linked with main_pages.css.scss, since I don't see any code in the header of application.html that links the two. Could someone explain how this link is made?
In you application.html.erb, add the following snippet somewhere in the head section:
<head>
<%= content_for?(:custom_styles) ? yield(:custom_styles) : '' %>
</head>
And then in your view file (e.g home.html.erb), you can add custom stylesheets to the current view with:
<% content_for :custom_styles do %>
<%= stylesheet_link_tag "custom_stylesheet_name" %>
<% end %>
Now everything you add to the content_for block in the page view file will be added to the header, so if you add a stylesheet link tag it will also be added to the header.
I'm having an image_tag that is supposed to display... an image. The problem is that if I set the src of the image to something that doesn't existe, it appear on the page (the broken link icon) but if I set the correct path to the image, nothing is displayed, even in the page source code... here is the code I'm using :
<% if #event.banner %>
<%= image_tag "/banners/" + #event.banner.path %>
<% end %>
I tried to put the relative path (like here), the absolute path with 'http://mywebsite.com/banners/y.jpg'.
The image is located in the public folder. It is reachable through the url I wrote above.
Thanks for your help.
EDIT : I tried with image_tag and <img /> tag
Still no idea of why I'm having this problem but it's not acting like this on another browser.
I'm a bit of a Ruby on Rails amateur, and I am trying to nest a div tag inside of an anchor tag in rails. I can make it work, but the resulting code I have written is terrible and is certainly NOT the rails way.
Here is an example of what I am trying to accomplish in HTML:
<a href="tell-a-friend">
<div id="tellafriend">
<strong>Strength in Numbers.</strong><br />
Suggest a friend or colleague to participate in this survey.
</div>
</a>
Here is what I came up with to do it in ERB:
<%= link_to content_tag(:div,
content_tag(:strong, 'Add your labor rates now.') +
content_tag(:br, '') + ' We are counting on you.', :id => 'participate'),
participate_path %>
Or here I mixed some HTML and ERB:
<%= link_to '<div id="results">
<strong>See the results.</strong><br />
Knowledge is power.
</div>'.html_safe, results_path %>
Both of my solutions seem very ugly... but moving it into a helper didn't seem like the right thing to do, given that the content of the DIV changes and that I am only displaying 3 of these on one page.
So if anyone is aware of a better way to do this, I am interested! Any combination of HTML, ERB and HAML is ok by me.
Links work as blocks:
<% link_to '', my_path do %>
<div></div>
<% end %>
FYI: An ANCHOR surrounding a DIV is not legal in HTML 4.01 Transitional (but it is in HTML5?) so make sure that you use the correct doc-type and test on the target browser(s)!
<head>
<title>a</title>
</head>
<body>
<div>
<a href="www.google.com">
<div>test</div>
</a>
</div>
</body>
</html>
You can run this at W3C Validator and change the DOCTYPE to see for which mode(s) this is valid. (Make sure to specify the encoding as well to get rid of the warning :-)
The worst is :
<%= link_to(url, html_options = {}) do %>
# name
<% end %>