Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: how to use scope with params and use route with default value of params

Tags:

I have such lines in routes.rb:

scope "/:subdomain/" do   resource :order, :only => [:new, :create, :show, :update, :edit, :destroy] do     get   :cancel,    :on => :member      put   :counter,   :on => :member    end end 

And for example, this is typical url: http://mydomain.com/some_subdomain/order/new . This url is mapped to action new of orders controller with params[:subdomain] = "some_subdomain". I want to use named route new_order_url(:subdomain => "some_subdomain").

But I want to map http://mydomain.com/order/new to orders controller, action new and params[:subdomain] = "default". And I want to use named route new_order_url for such url.

What is the best practices to do it?

like image 511
petRUShka Avatar asked Nov 02 '10 22:11

petRUShka


1 Answers

The answer is "use () and defaults"

scope "/(:subdomain)", :defaults => {:subdomain => "default"} do   ... end 

http://guides.rubyonrails.org/routing.html#dynamic-segments and http://guides.rubyonrails.org/routing.html#defining-defaults

like image 86
petRUShka Avatar answered Sep 30 '22 05:09

petRUShka