Rails 3 Route Nesting - ruby-on-rails-3

My Rails app routing looks something like this:
match 'Events/New' => 'event#new'
match 'Events/:date' => 'event#show', :date => /[A-Z][a-z]{2}-\d{2}-\d{4}/
match 'Events/:date/Venue' => 'event#venue', :date => /[A-Z][a-z]{2}-\d{2}-\d{4}/
match 'Events/:date/Speakers' => 'event#speakers', :date => /[A-Z][a-z]{2}-\d{2}-\d{4}/
Is there some way to nest this and clean it up? I suspect it's running the regex on :date multiple times, while nesting could theoretically make it run once.

Take a look at: http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing scope should solve your problem!
Something like:
scope :module => "events" do
// ...
end

Related

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

Routing with id and handle in Rails

I'm trying to set up rails to use both the ID and the Handle (which is just an URL safe version of the title) of a blog post in the route.
match '/articles/:id/:handle', :to => 'articles#show'
resources :articles
This works, of course -- but I can't seem to set up the to_param method in the model os the longer URL -- with the handle attached, is the default.
This doesn't work (not that I really expected it to):
def to_param
"#{id}/#{handle}"
end
I get a No route matches {:action=>"edit", :controller=>"articles", error. I also tried just using the handle, but then Rails generates links to the resource just using the handle and not the ID. I know I can do it with a - in stead of a /, but I prefer the /. Any way to make this work? If I have to add some extra paremeters to my link_to helpers, that's okay.
Did you try to pass a Hash to link_to?
link_to "Link", {:id => #article.id, :handle => #article.handle}
Update
You have to modify your routes:
match '/articles/:id/:handle', :to => 'articles#show', :as => :article_with_handle
and use the following helper to generate the link:
link_to "Link", article_with_handle_path(:id => #article.id, :handle => #article.handle)
You can override the helper to simplify things:
def article_with_handle_path(article)
super(:id => article.id, :handle => article.handle)
end
and use it like this:
link_to "Link", article_with_handle_path(#article)
Okay, here's what I did to remove the query string problem from the answer above:
Changed the route to this:
match '/articles/:id/:handle' => 'articles#show', :as => :handle
Removed the to_param method from the model and then generated the link like this:
link_to 'Show', handle_path(:handle => article.handle, :id => article.id) %>
That works, but could be condensed, obviously, with the helper above. Just change the one line to: args[1] = handle_path(:id => args[1].id, :handle => args[1].handle)

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...

How can I call different controllers from routes.rb

I'm just struggling a few hours with a problem, that seems rather easy, but not for me and not for google :)
I put some routes via
scope :path => '/:mandate_key', :controller => :tasks do
get '/' => :index #mandate_path
match '/import' => "import#index"
match '/clearance' => "clearance#index"
end
So far, so ugly! I'm looking for a way to call different controllers (import and clearance) dependent on the second param. Something like this:
scope :path => '/:mandate_key', :controller => :tasks do
get '/' => :index
scope :path => ':task_key', :controller => %{'task_key'}
get '/' => :index
end
end
where :task_key should be recognized as params[:task_key] and the called controller should be the value of params[:task_key]
So if a click a link like http://some.url/a_mandate_key/import it should call the ImportController.
I'm sure that the solution will be simple, but finding is hard!
Sometimes one is looking for a highly complicated solution, but it could be so much easier:
scope :path => '/:mandate_key' do
get '/' => "tasks#index" #mandate_path
get '/import' => "import#index"
get '/clearance' => "clearance#index"
end
Calling http://localhost/mandate the controller 'mandate' is called an params[:mandate_key] provides 'mandate'
Calling http://localhost/mandate/import the controller 'import' is called an params[:controller] provides 'import'
Trying the easy way is often the best way :)
Thanks for your help, Bohdan!
you might add in the bottom of your routes match ':controller(/:action(/:id))' so any unknown url will be dispatched this way
How about
scope :path => '/:mandate_key', :controller => :tasks do
get '/' => :index #mandate_path
end
....
match ':mandate_key/:controller(/:action)'
first scope will match routes /:mandate_key/tasks and the second one /:mandate_key/:controller or /:mandate_key/:controller/:action however second part should be defined in the bottom of your routes.rb file otherwise it'll match wrong routes

Better way to route this?

I have a routing issue. my routes currently look like this:
http://localhost:3000/events?category=popular&city=london&country=united-kingdom
Currently its just looking at resources :events (which is the events controller, index action), and in the index action. I've got a lot of if conditions. Especially, when the app needs to determine which partials to load, based on the query strings.
Is there a better way to refactor this? I am not a big fan of query strings in the URL. And something like this can be solved in routing. Just looking for the right direction. Am looking to have urls like:
/events/london
/events/london/popular
/events/london/united-kingdom
/events/united-kingdom
/events/united-kingdom/popular
/events/london/united-kingdom/popular
The Anti Patterns book, specifically on page 181, suggests creating separate controllers. Which will organize the code further and still keep it RESTFUL.
In theory, I think it is suggesting something like this:
match 'events/:city', :controller => 'events/cities', :action => 'index', :as => 'by_city', :via => :get
match 'events/:city/:category', :controller => 'events/cities_and_categories', :action => 'index', :as => 'by_city_and_category', :via => :get
match 'events/:city/:country', :controller => 'events/cities_and_countries', :action => 'index', :as => 'by_city_and_country', :via => :get
match 'events/:country', :controller => 'events/countries', :action => 'index', :as => 'by_country', :via => :get
match 'events/:country/:category', :controller => 'events/countries_and_categories', :action => 'index', :as => 'by_country_and_category', :via => :get
match 'events/:city/:country/:category', :controller => 'events/cities_and_countries_and_categories', :action => 'index', :as => 'by_city_and_country_and_category', :via => :get
OR, are filtered terms REALLY supposed to be in query string format?
If you have a suggestions / better approach. Do mention.
First off, some cleaner routes that are more canonical:
resources :events do
get '/:country(/:city)(/:category)' => 'events#filtered', :on => :collection, :constrain => { :category => %w{ popular legal_marijuana } }
end
I've made the country required. There are too many ambiguous city names, and unless you want to do a lot of poking to determine segment is what, this is just more straightforward. The city is optional, and so is the category.
Unless I've mistaken something here, this will constrain the category to either "popular" or "legal_marijuana" (but not both, so Amsterdam is out, haha).
All of these routes go to a single filtered action on your events controller. Don't bother trying to keep this resourceful unless it makes sense for every country and city to be a resource for your application. As long as this responds to a GET request, and it retrieves a collection of records, it's not very far out of the bounds of the REST principals (interesting note, the new and edit actions in Rails don't adhere strictly to REST either).
This action will need to do a bit of poking at params to see whether or not you have a city or category. You can use something like this to help you out:
#event = Event.where(:country => params[:country])
#event = #event.where(:city => params[:city]) if params[:city]
[For some reason this doesn't look right to me. I can't think straight. I'm sure that this is a mess. Someone please save me.]
But yeah, that's the idea.