Ruby on Rails 3: changing the default controller and the order of parameters in routing

I have a Rails application that has a controller named domainthat has a nested controller named subdomainand stats. I defined them in routes.rb:

resources :domains do
    resources :subdomains, :stats
end

I changed to_param of the domain and subdomain models to use the domain name, for example: the routing that I get http://site/domains/foo/subdomains/bar.

I would like to put it in order so that instead http://site/domains/foo/subdomains/barI can access it only with help http://site/foo/subdomains/bar. I tried the following in routes.rb:

match "/:id/" => "domains#show", :as => :domain

Which works great, but only gives me the opportunity to use the path http://site/foo, but for example http://site/foo/subdomains/barnot. I could create correspondence lines for each corresponding model and nested model, but it does nothing for other helpers except domain_url- that is, edit_domain_url points to /domains/foo/edit/instead /foo/edit.

Is there a way to change the routing so that it resourcesgenerates helpers pointing to the root URL without a part of the “domains”?

+3
source share
1 answer

match . . , . /domains/ , :

resources :domains, :path => "/" do
  resources :subdomains, :stats
end

config/routes.rb rake routes :

    domain_subdomains GET    /:domain_id/subdomains(.:format)         
    domain_subdomains POST   /:domain_id/subdomains(.:format)         
 new_domain_subdomain GET    /:domain_id/subdomains/new(.:format)     
edit_domain_subdomain GET    /:domain_id/subdomains/:id/edit(.:format)
     domain_subdomain GET    /:domain_id/subdomains/:id(.:format)     
     domain_subdomain PUT    /:domain_id/subdomains/:id(.:format)     
     domain_subdomain DELETE /:domain_id/subdomains/:id(.:format)     
         domain_stats GET    /:domain_id/stats(.:format)              
         domain_stats POST   /:domain_id/stats(.:format)              
      new_domain_stat GET    /:domain_id/stats/new(.:format)          
     edit_domain_stat GET    /:domain_id/stats/:id/edit(.:format)     
          domain_stat GET    /:domain_id/stats/:id(.:format)          
          domain_stat PUT    /:domain_id/stats/:id(.:format)          
          domain_stat DELETE /:domain_id/stats/:id(.:format)          
              domains GET    /(.:format)                              
              domains POST   /(.:format)                              
           new_domain GET    /new(.:format)                           
          edit_domain GET    /:id/edit(.:format)                      
               domain GET    /:id(.:format)                           
               domain PUT    /:id(.:format)                           
               domain DELETE /:id(.:format)                           

, !

+3

Source: https://habr.com/ru/post/1764286/


All Articles