D
D
Denis Zakorov2015-03-29 02:30:30
Ruby on Rails
Denis Zakorov, 2015-03-29 02:30:30

How to correctly resolve routing for different URLs?

Actually, I made it so that the project can be with a category. But at the same time, the project can be without a category. And immediately ran into a problem.
I can display and process either projects nested in a category or projects without a category.
routes.rb

get '../:project_url', to: 'project#index'
либо
get '../:category_url', to: 'project#category'
либо
get '../:category_url/:project_url', to: 'project#category'

How can I determine that now I have projects without categories, or a category with projects?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
caution, 2015-03-29
@caution

make paths for a project without a category and for a project with a category, and when rendering, write a helper that checks the object for the presence of a category and sets the desired path.
the first thing that came to mind

A
Andrey Andreev, 2015-03-29
@b0nn1e

Try

get '../:category_url/:project_url', to: 'project#category'
Raise higher in the code than other options, then this will have a higher priority.
The second variant of the "bad advice".
Add routes.rb to the very end
And then manually parse the url variable into parts in the controller (by the presence of "/"), look for matches in the database by project or category, if not found then manually raise an error
raise ActionController::RoutingError.new('Not Found')
and so on down the list.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question