Understanding How Rails (4.2) Configure Sites in Subdirectories by Category

Asked 2 years ago, Updated 2 years ago, 42 views

Sorry for the confusing title.

We would like to specify categories in subdirectories and run sites with the same configuration.
For example, example.com/car/articles lists articles about cars and example.com/car/shops lists car dealerships
example.com/bike/articles is a list of articles about motorcycles and example.com/bike/shops is a list of bike shops.

I'm afraid that I should do /categories/(car|bike), but I'm thinking of focusing the car's top page on example.com/car and the bike's top page on example.com/bike, so I'd like to do a restful URL design under /car, /bike.
By the way, regardless of car or bike, I am thinking of making the following configurations the same.

Currently

namespace:main, :path=>"/:category"do
  resources —articles
  resources:shops

You have configured routes as shown in , and when you attach a link,

=link_to "name", [:main,@article,:category=>@category_name]#@category_name=params[:category]

It is working in the form shown in , but I think it is redundant to specify the :category.
Is there a good practice that handles these cases well?

Please let me know.

ruby-on-rails ruby

2022-09-30 19:15

1 Answers

If I am in the same situation, I think I will write it as below.

%i(carbike).each do|category|
   namespace category do
     resources —articles
     resources:shops
   end
end

This will generate routes as follows:

car_articles GET/car/articles(.:format)car/articles#index
....
bike_articles GET/bike/articles(.:format)bike/articles#index

I haven't checked, but it might work below...?

=link_to "name", [@category_name, @article]


2022-09-30 19:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.