I'm trying to have a line in the head of my home page for my rails site. This should be:
<link rel="canonical" href="http://negotiatus.com" />
On my personal dev copy of the site, it appears perfectly in the head of the home page. However, on the production version of the site it does not. I believe it has something to do with Rails precompiling stuff, but I could be wrong.
If someone could point me in the right direction here that would be great
Relevant part of the home view:
<% content_for :for_head do %>
<link rel="canonical" href="http://negotiatus.com" />
<% end %>
In my application.html.erb:
<% if content_for? :for_head %>
<%= yield :for_head %>
<% end %>
It shows up if I inspect the source of my local dev copy, but doesn't show up if I view the source of the production version (hosted on heroku).
I retried precompiling everything, then committed and pushed everything again, now it works.
Sorry for wasting anyone's time.
Related
Lately, I've been making my site more SEO friendly. Everything has been fun and games
until the canonical tag came along.
My site index is structured like the following.
example.com/all show all products.
example.com/all?brand=levis shows all products from Levi's.
example.com/jeans shows all jeans.
I would want to have dynamic canonical tags that refer to mysite.com/all, mysite.com/all?brand=current-brand and mysite.com/current-category.
The site also has a filter option for color and brands which produces slugs like mysite.com/hoodies?color=grey and mysite/hoodies?brand=nike&color=green. In both cases, I would want to have a canonical referring to mysite.com/hoodies
I made an attempt to fix this with an if else solution without success.
How it's set up now is like the following:
views/layouts/application.html.erb
<%= yield :canonical %>
views/pages/about
<%= content_for :canonical do %>
<link rel="canonical" href="example.com/about/" />
<% end %>
views/products/show
<% content_for :canonical do %>
<link rel="canonical" href="<%="example.com/#{#product.id}/"%>" />
<% end %>
These two works fine but when it comes to the index page it becomes trickier.
First I tried
<% content_for :canonical do %>
<link rel="canonical" href="<%="example.com/#{#category.slug}/"%>" />
<% end %>
This solution works fine when you go by category but crashes when you go to mysite.com/all or mysite.com/all?brand=levis.
I tried to experiment with some if else statements where I tried to target the current slug but it all ended up with crashes.
Do you guys have any suggestions of what I can do?
I somewhat solved the problem by placing
<% content_for :canonical do %>
<link rel="canonical" href="<%="example.com/#{#category.slug}/"%>" />
<% end %>
Under
<% if #category %>
It does not work exactly as I described in the questing since there is no canonical for example.com/all?brand=levis nor example.com/all. But that might even be better for SEO since google might take /all and /jeans for duplicate content, I'm not 100% sure. If you have another better solution, share and I would be more than happy!
I am following along with the 'Agile Web Development with Rails 4' guide and have come to a section on caching. Here are the steps to follow:
in config/environments/development.rb
config.action_controller.perform_caching = true
In app/models/product.rb
def self.latest
Product.order(:updated_at).last
end
in views/store/index.html.erb
<% cache ['store', Product.latest] do %>
<% #products.each do |product| %>
<% cache ['entry', product] do %>
<div class="entry">
<%= image_tag(product.image_url) %>
<h3><%= product.title %></h3>
<%= sanitize(product.description) %>
<div class="price_line">
<span class="price"><%= number_to_currency(product.price) %></span>
</div>
</div>
<% end %>
<% end %>
<% end %>
To verify the cash is working, the book says: "As far as verifying that this works, unfortunately there isn't much to see. If you go to that page, you should see nothing change, which in fact is the point! The best you can do is to make a change to the template anywhere inside the cache block without updating any product and verifying that you do not see that update because the cached version of the page has not been updated".
However, when I try and add a "Hello" string in the code within the cache blocks, it appears on the page. I've done all the server restarting and what not.
But, when I reload the page on my local host I do see this line
Cache digest for app/views/store/index.html.erb: 6c620ede1d4e824439a7b0e3b177620f
which is not their when I have
config.action_controller.perform_caching = false
Link to git hub repo: https://github.com/BrianLobdell/depot
Thank you,
Brian
Rails updates the cache when changing the file, so it's easier to manipulate the cache instead.
You can retrieve the cache fragment using the cache digest for the page in your Rails console (the digest might have changed, make sure to use the recent one!):
key = ['store', Product.latest, '6c620ede1d4e824439a7b0e3b177620f']
ActionController::Base.new.read_fragment(key)
This should return the cached fragment. To ensure that Ruby actually hits the cache when serving the page, you could replace the fragment with some other content:
ActionController::Base.new.write_fragment(key, 'it works!')
Reload the page, you should see "it works!".
I am working on a Rails 4 app in application.html.erb I have the following code
....
<head>
<title>...</title>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= yield :head %>
<%= csrf_meta_tags %>
</head>
What this does is allows me to load certain assets depending on the page I am on. So maybe in the show template of a certain action I could do something like
<% content_for :head do %>
<%= stylesheet_link_tag "show" %>
<% end %>
This works great and helps me keep the page structured clean and simple. The issue I am having now is that turbolinks is completely ignoring these assets and not even loading them when I click on a next link. I have looked around but cant seem to find an answer to this problem. Any help will be appreciated.
I had the same problem, try to add data-turbolinks-track into your stylesheet tag
This is most likely because turbolinks does not replace the <head> element:
Turbolinks makes following links in your web application faster. Instead of letting the browser recompile the JavaScript and CSS between each page change, it keeps the current page instance alive and replaces only the body and the title in the head. Think CGI vs persistent process.
As such, any changes to the <head> element will be ignored.
I made an app in RoR and it's working perfectly locally, but there's only one page that's not working when I upload to Heroku. It's a view that has only this in the code:
<h1>Listagem</h1>
<% #items.each do |item| %>
<%= link_to item.materia, public_path(item) %>
<% end %>
<br />
I noticed that if I remove the public_path(item) the page works, so, is there any alternative to this?
THe only thing that sticks out to me is that you may have misspelled material in
<%= link_to item.materia, public_path(item) %>
this would throw an exception. You might want to check your logs as well
I am using a Rails 2.3.8 application. I have a layout file that contains
<%= yield :head %>
I called the following code in different parts of my application:
<% content_for :head do %>
<meta name="keywords" content="" />
<% end %>
I tried to add that code in a view file home it worked. I called it from patial _abc that is called from home it also worked as expected by adding the meta code in the head part. But when I added the code inside partial _def that is called from _abc which is called from home the meta tag did not show up!!!
I didnt usderstand what is going on... and how can bypass this problem
UPDATE: The case that did not work was:
home.html.erb:
<%= render :partial=>"_abc"%>
_abc.html.erb:
<%= render :partial=>"_def"%>
_def.html.erb:
<% content_for :head do %>
<meta name="keywords" content="" />
<% end %>
Thanks a lot
This is an old question, but I got here with a similar issue. Figured I'd post my issue/solution in case someone else lands here.
Which file is <%= yield :head %> in? I had this issue and it turned out my yield was in the "/layouts/application.html.erb" file... But the view being called was using a different layout file i.e. "/layouts/listings.html.erb". once I moved the yield into the correct file, everything worked fine, and I had pretty much the same setup you described.