B
B
bismoney2015-11-16 20:57:19
Ruby on Rails
bismoney, 2015-11-16 20:57:19

Date in URL repeat, didn't figure it out?

So I could not figure it out .... I ask for help friends.
Now there is routes.rb

resources :post_categories, path: '', only: :show do
   resources :posts, path: '', only: :show
end

And the handler in the post_controller controller, where we pull the category by post_category_id and the post itself by id
def show
@posts = PostCategory.find_by!(seo_url: params[:post_category_id]).posts.find(params[:id])
end

Thus, getting the path
site.com/category_name/id
Now I need to get this path
site.com/category_name/year/month/day/id
Where year/month/day is the date of the id post
In routes.rb I prescribe the following construction
resources :post_categories, path: '', only: :show do
   resources :posts, path: '/:year/:month/:day', only: :show, constraints: { year: /\d{4}/, month: /\d{2}/, day: /\d{2}/ }
end

But for the life of me, I can't figure out how to handle these parameters in the controller... It's
params[:year], params[:month], params[:day]
not hard to help anyone.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vsuhachev, 2015-11-17
@bismoney

You have some kind of oil. In order to get a Post, it is enough to pass its id. Why do you need another category and date in the url, I don’t understand. Typically, if a url like site.com/category_name/year/month/day is used, it will return a group of objects filtered by category and date. If you need such behavior, add the index method to the PostsController and set up such a route to it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question