Rails routes with date - ruby-on-rails

My question it's about use a filter by date in the route of a rails application, at the moment I all ready have the rule that match the pattern of the date in the routes.rb file, it's looks like this:
match "itineraries/:day/:month/:year" => "itineraries#index",
:constraints => { :year => /\d{4}/, :month => /\d{2}/, :day => /\d{2}/ }
match "itineraries/new/:day/:month/:year" => "itineraries#new",
:constraints => { :year => /\d{4}/, :month => /\d{2}/, :day => /\d{2}/ }
resources :itineraries
match '/:controller(/:action(/:id))'
root :to => "itineraries#index"
That match for example /itineraries/01/01/2011, the problem comes when I generate a route from the resource, for example, itineraries_path(:year=>2011,:month=>1,:day=>1) generate:
/itineraries?day=1&month=1&year2011
instead of
/itineraries/01/01/2011
Is there a way to put the match rule inside the resource mapping?

well, short answer is your route does exists, but it's not named yet (check rake routes to convince yourself) and that's why you can't call it just like that (using itineraries_path)
so, the correct way to do it would be for example:
match "itineraries/:day/:month/:year" => "itineraries#index",
:constraints => { :year => /\d{4}/, :month => /\d{2}/, :day => /\d{2}/ },
:as => "itineraries_date"
(notice the :as part)
now, if you call itineraries_date_path(11,12,1998) it will give you itineraries/11/12/1998

Related

can't add crud actions to event_calendar?

I added the gem 'event-calendar', :require => 'event_calendar'
bundled
rails generate event_calendar
migrated
changed
match '/calendar(/:year(/:month))' => 'calendar#index', :as => :calendar, :constraints => {:year => /\d{4}/, :month => /\d{1,2}/}
to
get '/calendar(/:year(/:month))' => 'calendar#index', :as => :calendar, :constraints => {:year => /\d{4}/, :month => /\d{1,2}/}
went to
http://localhost:3000/calendar/
and I got myself a pretty blingin calendar, was just wondering how do you now add crud actions to it? I have been looking at the documents and find it rather confusing.
You're probably going to want to make an Events Controller and define the CRUD actions inside there.

Rails routing sending non static default value

I am upgrading application from rails 2.3/x to 3.2.x
I have a old route like this:
map.calendar '/calendar/:year/:month', :controller => 'calendar', :action => 'index', :year => Time.now.year, :month => Time.now.month
rake routes generate something like this:
calendar /calendar/:year/:month {:controller=>"calendar", :action=>"index"}
I am not sure what is year and month here, are they default values being sent in case none is given? What would be its rails 3.2.x syntax.
I tried something like this:
match '/calendar/:year/:month' => 'calendar#index', :defaults => {:year => Time.now.year, :month => Time.now.month}
The route generated in rails 3.2.x is:
/calendar/:year/:month(.:format) calendar#index {:year=>2013, :month=>9}
Help me out to convert this route correctly.
You should add as key to your match call:
match '/calendar/:year/:month' => 'calendar#index', :defaults => {:year => Time.now.year, :month => Time.now.month}, :as => 'calendar'

Rails routing, matching short urls?

I'm having an issue where I've created a route I'm using to match short token like urls, like this:
myapp.com/a2c3b
I'm doing that by using a route like this:
match '/:id' => 'items#show', :as => "show_item", :via => :get, :constraints => { :id => /[a-z0-9]{5}/ }
But the issue is that now my other routes like /admin don't work because that also has 5 characters, how can I work around this, and have both kinds of routes work?
Put all of your routes that would match before this route in the file... that is...
match '/admin'....
match '/login'....
match '/:id' => 'items#show', :as => "show_item", :via => :get, :constraints => { :id => /[a-z0-9]{5}/ }

Subdomain for nested resources in Rails

My rails app is set to use subdomains as described in this RailsCast:
http://railscasts.com/episodes/221-subdomains-in-rails-3
However, right now, paths render like this:
http://organization.domain.com/organizations/1/edit
I have the controllers set up to choose the organization based on the subdomain already, so I'm wondering if there's a way to strip out the /organizations/:id portion of the paths, such that:
link_to edit_organization(#organization)
goes to http://organization.domain/edit, instead of http://organization.domain/organizations/:id/edit
Since there's going to be many nested resources within organizations (people, donations, etc), it's important that URL's don't end up incredibly long, and that the path generation method remains pretty straightforward.
Is there a way to do this?
You could use a route like:
resource :organization, :path => ""
The will cut down your url to 'http://organization.domain/:id/edit`.
Getting rid of the :id is tricky, and I dont think it can be done directly. What I would do is something like:
resource :organization, :path => "", :only => [] do
match "index", :via => :get
match "new", :via => :get
match "show", :via => :get, :constraints => {:subdomain => /[a-zA-Z]+/}
match "edit", :via => :get, :constraints => {:subdomain => /[a-zA-Z]+/}
match "update", :via => :put, :constraints => {:subdomain => /[a-zA-Z]+/}
match "create", :via => :post
end
Not very DRY, but I think it should work.

How do I 301 redirect these route changes?

I have a couple routes like this:
match ':category/:brand/:permalink' => 'products#show', :as => :public_product
match 'stoves' => 'home#stoves', :as => :stoves
I changed them to this:
match ':category/:brand/:permalink' => 'products#show', :as => :public_product
match 'wood_stoves' => 'home#wood_stoves', :as => :stoves
I changed the category record titled stoves to wood_stoves.
Can I add a route redirect that allows for wildcards that would change anything like domain.com/stoves or domain.com/stoves/morso/8140-contemporary to domain.com/wood_stoves or domain.com/wood_stoves/morso/8140-contemporary, respectively? Or should I put this in my apache virtualhost config block?
Let your route.rb to be like this:
match ':category/:brand/:permalink' => 'products#show', :as => :public_product
match 'stoves' => 'home#stoves', :as => :old_stoves # anything else except stoves
match 'wood_stoves' => 'home#wood_stoves', :as => :stoves
In your Home Controller:
def stove
redirect_to stoves_path(# Use parameters here when a user hits a link like domain.com/stoves or domain.com/stoves/morso/8140-contemporary)
end
def wood_stoves
# your code here..
end
match ':category/:brand/:permalink' => 'products#show', :as => :public_product
match 'stoves' => 'home#stoves', :as => :old_stoves #
match 'wood_stoves' => 'home#stoves', :as => :stoves
This is enough i think.
If you are so particular about to change the name of the action to wood_stoves then i think you need to do Surya's solution

Resources