how to not show default locale in url for rails 3.0.11 app using translate_routes gem - ruby-on-rails-3

I have a rails 3.0.11 application.
I am using the translate_routes gem which seems to have a bug so I can't do wildcard matches with locales as follows:
routes.rb
MySite::Application.routes.draw do
.
.
.
match '/:locale/*path' => 'site#show', :as => 'cms'
ActionDispatch::Routing::Translator.translate_from_file('config/locales/routes.yml')
end
SO I have had to add the following:
ActionDispatch::Routing::Translator.translate_from_file('config/locales/routes.yml')
match '/(:locale)/*path' => 'cms#show', :as => 'cms', :locale => /fr|ar|en/
This works in so much as the paths have the locales and the system can find the routes. However it shows
en/somepage
when I want
/
for the default.
Any ideas on how to not show the default locale?

Have you tried overwriting default_url_options like this?
def default_url_options(options={})
options.merge!({ :locale => ((I18n.locale == I18n.default_locale) ? nil : I18n.locale) })
end

Related

Setting Devise omniauth_path_prefix doesn't work

I'm working on a Rails-based API. I recently started attempting to version it. (I'm using the Versionist gem, in case it matters) One version ('v2') uses Devise and Omniauth to authenticate users through Facebook/Twitter.
I want all the routes associated with this version to have the appropriate version prefix (so users/:username/foo becomes v2/users/:username/foo, etc.), but I've already found out that putting devise_for inside the api_version block prevents the Devise helpers (current_user, user_signed_in?, etc.) from working, so it continues to live outside the block:
routes.rb:
devise_for :user, :path => '', :controllers => {:omniauth_callbacks => 'users/omniauth_callbacks'}, :skip => [:registrations, :confirmations, :sessions, :passwords]
api_version(:module => "V2", :path=>"v2") do
resources :authentications, :only => [:update, :destroy]
devise_scope :user do
post 'login' => 'sessions#create', :as => 'user_session'
get 'logout' => 'sessions#destroy'
post 'password' => 'devise/passwords#create'
put 'password' => 'devise/passwords#update'
end
end
Everything seemed great... except the Devise-generated omniauth routes:
rake routes output:
user_omniauth_authorize /auth/:provider(.:format)
user_omniauth_callback /auth/:action/callback(.:format)
Now, some google-fu revealed that there's a devise configuration setting for this, so I added the following to our devise initializer (config/initializers/devise.rb):
Devise.setup do |config|
config.omniauth_path_prefix = 'v2/auth'
end
Now, rake routes produces paths that look sensible:
user_omniauth_authorize /v2/auth/:provider(.:format) v2/users/omniauth_callbacks#passthru {:provider=>/(?!)/}
user_omniauth_callback /v2/auth/:action/callback(.:format) v2/users/omniauth_callbacks#(?-mix:(?!))
However, when I attempt to access this route by calling api.localhost/v2/auth/facebook, I get a routing error:
ActionController::RoutingError (No route matches [GET] "/v2/auth/facebook")
Any idea what's going on here?
You are missing the provider name in the routes so they don't match the facebook part in /v2/auth/facebook. The correct route destination should look something like v2/users/omniauth_callbacks#(?-mix:facebook).
Have you specified the provider in the user model?
devise_for ..., :omniauthable, :omniauth_providers => [:facebook]
For the record, I'm using Rails 3.2 and Devise 3.0 and the altered route seems to work (I haven't gone further yet to see if something else will break).

Rails 3 - routing to a controller method

heres what I'm currently using:
Rails -v 3.2.3
I'm trying to make a path in routes.rb that will route to a method in my links_controller.rb.
routes.rb
match 'modify_points' => 'links#modify_points', :as => :modify_points
links_controller.rb method
def modify_points
#link = Link.find(params[:id])
#link.update_attribute :points, #link.points + params[:by].to_i if params[:by] =~ /[+|-]?1/
render_text #link.points
end
here is how i am calling the method in my view:
<%= link_to('UP', modify_points_path(link, :by => 1), :remote => true) %>
however when i click on this UP link.... nothing happens. what should happen is link.points should increase by 1, however when i check in the console, my points are still at zero. am i creating the routes correctly?
I have always used something like this:
match '/modify_points' => 'link#modify_points', :as => 'modify_points'
Does that generate the route you want? What does rake routes show?

Routes problem with I18n_routing gem

I'm on Rails 3 and on my 2nd Rails project (i.e. I'm a newbie). I am making a website with several locales, at the moment Swedish and US. I am using the I18n_routing gem to create localized url:s. I am also using the friendly_id gem to create better urls.
My problem: I cannot get my nested urls to be translated. They remain as the default-urls.
This is my routes.rb:
localized(I18n.available_locales, :verbose => true) do
resources :calculation_types, :only => [:show], :path => '' do
resources :calculations, :only => [:index, :show], :path => '' do
member do
put 'calculate_it'
get 'calculate_it', :redirect_me => true
get 'link'
end
end
end
end
localized(I18n.available_locales, :verbose => true) do
match 'searchresults' => 'home#search-results', :as => :searchresults
match 'about' => 'home#about', :as => :about
match 'advertise' => 'home#advertise', :as => :advertise
match 'terms' => 'home#terms', :as => :terms
match 'calculator' => 'home#calculator', :as => :calculator
match 'feedback' => 'home#feedback', :as => :feedback
end
This is a sample (cut) of my locale (for Swedish):
se:
named_routes_path:
about: 'om'
advertise: 'annonsera'
calculator: 'kalkylator'
feedback: 'feedback'
searchresults: 'sokresultat'
terms: 'anvandaranvisning'
resources:
accumulated-passive-income: "vardet-av-din-passiva-inkomst"
all-about-a-date: "allt-om-ett-datum"
area: "area"
average-speed: "genomsnittshastighet"
birthday-in-days: "fodelsedag-i-dagar"
These are some facts of the case:
The resources-translations include both "calculation_types" and "calculations".
I have tried several set-ups in the translation file.
The SECOND routing WORKS, the one with the "match", they also appear when I do rake routes.
I get no error messages. Everything works fine.
I am using friendly_id as the url-words. An example of a url could be http://local.domain.com:3000/diet/bmi where "diet" is calculation_type.friendly_id and bmi is calculation.friendly_id
I want help with:
- Why do the nested routes not show up as routes? Why are they not being created?
- How do I get this to work?
Do you need any more info to help me?
It seems like it is not a matter of I18n_routing after all since the words that should be translated are actually friendly_id:s. So, disregard the I18n_routing part of this problem and focus on the friendly_id-translation...

Rails url_for Gravatar routing error

This function is defined in the application_help.rb:
def gravatar_url_for(email, options = {})
url_for(
{
:protocol => 'http://',
:host => 'www.gravatar.com',
:controller => 'avatar',
# :controller => 'avatar.php',
:gravatar_id => Digest::MD5.hexdigest(email),
:only_path => false
}.merge(options)
)
end
It's used in views:
<%= image_tag(gravatar_url_for user.email, {:d => 'identicon', :s => 32, :r => 'g'}) %>
Occasionally, its usage will result in a routing error:
No route matches {:controller=>"avatar", :d=>"identicon", :r=>"g", :gravatar_id=>"486575e581db04b7c8ca218af8488657", :s=>32}
A valid email is being supplied when the error occurs.
If I replace the url_for() with this logic, it works as expected:
url_for("http://www.gravatar.com/avatar/" + Digest::MD5.hexdigest(email) + "?d=identicon&s=40&r=g")
** edit **
I had removed the following line from the routes.rb file:
# This is a legacy wild controller route that's not recommended for RESTful applications.
# Note: This route will make all actions in every controller accessible via GET requests.
match ':controller(/:action(/:id(.:format)))'
Is there a way to get the url_for to work without the 'legacy wild controller route'?
You might want to take a look at the Gravtastic plugin for Rails which supports Gravatar images in both Ruby and JavaScript.

Rails 3 I18n routes

I have such routes in my app:
# config/routes.rb
Demo::Application.routes.draw do
root :to => "requests#index"
match 'find' => 'requests#find'
get "about/developer"
get "about/api"
end
All works ok.
But I want to enable I18n urls and changed routes: (by the official Rails guide):
# config/routes.rb
Demo::Application.routes.draw do
scope "(:locale)" do
root :to => "requests#index"
get "about/developer"
get "about/api"
match 'find' => 'requests#find'
end
end
After adding scope lines it gives error:
Exiting
C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/actionpack-3.0.3/lib/action_dispatch/routing/mapper.rb:160:in
`default_controller_and_action':
missing :controller (ArgumentError)
What's up? Official guide is wrong?
My Rails version: 3.0.3, Ruby 1.8.7
Does it work if you specify all of the controller/action names?
In other words, try changing:
get "about/developer"
get "about/api"
to:
get "about/developer" => "about#developer"
get "about/api" => "about#api"