Rails 3 I18n routes - ruby-on-rails-3

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"

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 routes locale, gem 'route_translator' or gem 'i18n_routing' unable to translate match in resource

mainly i would use i18n_routing gem
i will be happy if i will translate match 'vlk' under dashboard resource, trying everything around, and no success. Resources are translated successfully also new action...whats wrong?
routes.rb
MyApp::Application.routes.draw do
root :to => 'home#index'
localized do
resources :cars
resource :admin, :controller => :admin
resource :dashboard do
member do
match 'vlk', :as => :vlk
end
end
end
end
routes.yml
cs:
cars: 'auta'
admin: 'admincesky'
routes:
dashboard:
as: 'novy'
path_names:
new: 'cesky_member'
vlk: 'tzz_cesky'
named_routes_path:
vlk: 'tzz_cesky'
2.secondly, i tried next gem 'route_translator' and i cant get running 'route_translator' gem i am getting this error
/ruby-1.9.2-p320/gems/actionpack-3.0.14/lib/action_dispatch/routing/route.rb:25:in `initialize': can't convert Array into String (TypeError)
if i add line at the end of routes.rb file
Dt::Application.routes.translate_from_file('config/locales/routes.yml')
I had the same problem.
I tried a lot - without any solution.
I tried this gem https://github.com/kwi/i18n_routing - that fixed my problems :-)

how to not show default locale in url for rails 3.0.11 app using translate_routes gem

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

Is a route like `"controller#action"` not the same as `:controller => 'controller'` and `:action => 'action'`?

I have a Rails 3.1 app where I just got some weird behavior.
I had two routes declared as follows:
# OLD METHOD
get 'accept_terms', :to => "users_terms#show"
put 'accept_terms', :to => "users_terms#accept'"
Running rake routes included the following:
# accept_terms
# GET /accept_terms(.:format) {:action=>"show", :controller=>"users_terms"}
# PUT /accept_terms(.:format) {:action=>"accept'", :controller=>"users_terms"}
The GET worked fine, but the PUT produced this error:
AbstractController::ActionNotFound (The action 'accept'' could not be found for UsersTermsController):
I confirmed that the action did exist on that controller.
While tinkering around with the problem, I changed the route declarations to:
get 'accept_terms', :controller => 'users_terms', :action => 'show'
put 'accept_terms', :controller => 'users_terms', :action => 'accept'
Running rake routes produced:
# accept_terms
# GET /accept_terms(.:format) {:controller=>"users_terms", :action=>"show"}
# PUT /accept_terms(.:format) {:controller=>"users_terms", :action=>"accept"}
With this, both GET and PUT worked fine.
Is :to => "controller#action not the same as :controller => 'controllerName', :action => 'actionName'?
The only difference I see in the produced routes is the order of :action and :controller...
In the old method you end the users_terms#accept with both a single quote and a double quote.

Rails 3.1 No route matches controller and action but they exist

I'm playing with a test rails app. However I stumbled on a problem I can't resolve.
I've a users controller and in it there's an activate method.
In the routes.rb file I have
match 'activate/:email/:validation_code' => 'users#activate', :as => :activate_user, :via => :get
After that I try to use activate_user_path(#user) but a routing error is raised.
No route matches {:controller=>"users", :action=>"activate", :email=>#<User id: 12, email: "someone#test.test", validation_code: "zbBPLQUsBgPvEJfcjxmXuxFxuJAKEoqQNASkbybihpnmzSbhxdC...", active: false, created_at: "2011-11-10 14:56:23", updated_at: "2011-11-10 14:56:23">}
Running rake routes shows the routing is there:
activate_user GET /activate/:email/:validation_code(.:format) {:controller=>"users", :action=>"activate"}
I tried searching for this problem, but usually this happens when you forget to pass the object that's needed to build the route.
All help is appreciated :)
I would make sure you are actually invoking a GET and not a POST as the method.
If you are submitting a form, it defaults to POST, which would mean you don't have a matching route, the route you listed above only works for GET requests.
Does activate_user_path(:email => #user.email, :validataion_code => #user.validation_code) works for you?
EDIT
I don't know why, but when I've changed:
validation_code to code it works.
match 'activate/:email/:code' => 'users#activate', :as => :activate_user, :via => :get
Probably in rails you can't use var names in routes with underscore, but this has to be verified.