Wednesday, May 13, 2009

ruby on rails, googlebot(at)googlebot.com, No HTTP_REFERER, canonical and consistent websites



When you have an exception from the system, something like:


Subject: [ERROR] ships#flag_choice (ActionController::RedirectBackError) "No HTTP_REFERER was set in the request to this action, so redirect_to :back could not be called successfully. If this is a test, make sure to specify request.env[\"HTTP_REFERER\"]."


A ActionController::RedirectBackError occurred in ships#flag_choice:

No HTTP_REFERER was set in the request to this action, so redirect_to :back could not be called successfully. If this is a test, make sure to specify request.env["HTTP_REFERER"].




Is your chance to make a better website, a browseable website for robots.



Here you have:

app/controllers/application.rb

before_filter :load_url_back

def flag_choice
set_locale(params[:id])
session["lang"] = "#{params[:id]}"
#redirect_to :back
redirect_to params[:url_back]
end

def load_url_back
@url_back = "http://" + request.env["HTTP_HOST"]
+ request.env["REQUEST_URI"]
end


apps/views/layouts/application.html.erb

<%= link_to(image_tag('ingles.png'),
:action => 'flag_choice', :id => 'en_EN',
:url_back => @url_back ) %>
<%= link_to(image_tag('esp.png'),
:action => 'flag_choice', :id => 'es_AR',
:url_back => @url_back ) %>





Now we have consistent links that can be saved in any place, and consulted without history of browser navigation. This makes your links independent from where you came from.
And in the controller you are working in a request that just need canonical information from the HTTP Request.

No comments: