Can controller names on RESTful routes be optional?

With standard routing mechanics map.resourceand a few nested resources, the resulting routes are unnecessarily long. Consider the following route:

site.org/users/pavelshved/blogs/blogging-horror/posts/12345

It's easy to create in routes.rb, and I'm sure it follows some useful routing logic. But it is too long and it also seems that it is not intended for human reading.

A good improvement would be to reset the controller names, so it looks like this:

site.org/pavelshved/blogging-horror/12345

Clear, simple, short. This may become ambiguous, but in my case, for example, I will not call users "users".

I tried to install :as => '', but it gives such routes: site.org//pavelshved//blogging-horror//12345when they are created by standard helpers.

Is there a way to map resources so that controller names become optional?

+3
source share
5 answers

You are looking for a parameter: path_prefix for resources.

map.resources :users do |user|
  user.resources :blogs do |blog|
    blog.resources :posts, :path_prefix => '/:user_login/:blog_title/:id'
  end
end

Will create a quieter routes for all the blogs of the form: site.org/pavelshved/bogging-horror/posts/1234. You would have to make extra efforts to use the URL helpers, but not one of your own shells could fix it quickly.

The only way to get rid of some of the messages in the URL is with named routes, but this requires some duplication to calm down. And you will encounter the same problems when trying to use route assistants.

+4
source

, , - RESTful, :

map.short_blog ':user_id/:blog_id/:id', :controller => 'posts', :action => 'show'

URL, , . , URL-, short_blog_ *.

+2

default.rb:

map.connect 'products/:id', :controller => 'catalog', :action => 'view'

:

map.connect ':user_id/:blog_id/:id', :controller => 'posts', :action => 'show'

, , URL-.

+1

map.pavelshved '/pavelshved/', :controller => :users, :action => view or
map.pavelshved '/:id', :controller => :users, :action => show do | blogs|
  blogs.bloging '/:id', :controller => :blogs, :action => show do | post|
    post.posting '/:id', :controller => :posts, :action => show
  end
end

, :)

+1

Google " " .

-1

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


All Articles