Thierry Lam
Thierry Lam

Reputation: 46294

How can I pass additional parameters to a resource route in Rails?

I have a model called Post. In config/routes.rb, I defined its route as:

resources :post

Everything works fine with the default paths. I can create a new post at the following url:

/posts/new

I need to pass additional parameters so that the new url becomes:

/posts/new/:year/:month/:day

If I do the following, it assumes a post_id should exists:

resources :posts do
  match '/new/:year/:month/:day',         
    :to => 'posts#new',
    :constraints => {:year => /\d{4}/, :month => /\d{2}/, :day => /\d{2}/},
    :as => 'new_post'
end

For the above, rake routes give me:

/posts/:post_id/new/:year/:month/:day(.:format)

How can I configure the default new path to pass additional parameters?

Upvotes: 1

Views: 1203

Answers (1)

Valery Kvon
Valery Kvon

Reputation: 4496

...
match '/new/:year/:month/:day', :on => :new
...

Upvotes: 4

Related Questions