Rails routing sending non static default value - ruby-on-rails

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'

Related

Rails 2, define route with a param containing slashes and dots

I would like to define URLs like these ones:
/pages/VAR.1
/pages/VAR/1
So I try different configs (but none of them works):
map.page 'pages/:id', :controller => 'pages', :action => 'custom', :constraints => { :id => /[^\/]+/ }
map.page 'pages/:id', :controller => 'pages', :action => 'custom', :constraints => { :id => /(.*)+/ }
and the application redirects to 404 page. How should I do?
I'm working with ruby 1.8.7 and rais 2.3.18
Thanks in advance
Oh... my fault! I'm used with rails3, but rails2 uses "requirements" instead of "constraints".

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.

Changing Rails 2 Routes to Rails 3 Routes

Hi I'm trying to change my routes from a rails 2 project to match the syntax in rails 3. How would I change this:
map.connect "/stylesheets/:action.css",
:controller => "stylesheets",
:format => "css"
so that the :action can be matched to an action from the stylesheets controller?
Would it be something like this?
match 'stylesheets/:action.css', :to => "stylesheets#{:action}"
Also, what is the syntax for :format in the routes for rails 3?
You're almost there with your solution. The only thing you have to change is the way you reference :action in the :to value.
match 'stylesheets/:action.css', :to => 'stylesheets#:action', :format => :css
As you can see, the syntax for :format didn't change.
I think it would be smth like:
match 'stylesheets/:action', :controller => :stylesheets, :defaults => { :format => :css }
or you can constraint your routes to .css format using :constraints => { :format => 'json' }
I recommend you to read through http://guides.rubyonrails.org/routing.html

Rails routes with date

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

Rails 2 routes to rails 3 conversion

does anyone know if there is a rails routes converter online? I was not able to find one. I am trying to convert this line:
map.add_payment_profile 'add_payment_profile/:id', :controller => 'payment_profile_controller', :action => 'add_payment_profile'
Thanks!! So if I understand correctly:
map.create_cim_payment_profile 'create_cim_payment_profile_user', :controller => 'authorize_net', :action => 'create_cim_payment_profile', :only => :post would be
match 'create_cim_payment_profile_user' => 'payment_profile#create_cim_payment_profile', :as => :create_cim_payment_profile
I believe what you're looking for is
match 'add_payment_profile/:id' => 'payment_profile#add_payment_profile',
:as => :add_payment_profile
Rails 3 has a new controller#action shorthand for mapping controller actions. You will also need to specify :as to created a named route.
may be this will work:
match 'add_payment_profile/:id',:controller => "payment_profile_controller",
:action => "add_payment_profile"

Resources