0

I have a WP site with "/page/" routing pattern for static pages and "/category/" for categories. So WordPress understands that "/about/" is a page and "/science/" is a category. Now I'm trying to move it on Rails with same routing. Both "Page" and "Category" are models and they should be able to be edited from an admin area. How can i make it clear for Rails to check for existing page and if there is no page then check for a category?

P.S. I know I could do '/pages/:url/' but I'm really interested in how to do what i've asked before.

3
  • 1
    you should really start with guides.rubyonrails.org/routing.html Commented Aug 4, 2014 at 12:09
  • Do you have lots of static pages? Because if not, you can do specific routes for them and another route for categories. Commented Aug 4, 2014 at 12:15
  • I did it. I know some basics of rails routing. I don't think I will have many static pages. Yes, I can implement individual route for every existic static page, but if I add one more in the admin area there won't be a route for it. Commented Aug 4, 2014 at 20:55

2 Answers 2

0

You didn't gave details about your code, so I'm going to write a generic answer. You can adjust to your code.

In most cases we have few static pages, like about, home and maybe something more

So, based on that, you can make specific routes for these static pages, and another route to categories.

get '/home'      => 'static#action'
get '/about'     => 'static#anotheraction'
get '/:category' => 'categories#action'
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I could make specific routes for every single page and after that make a dynamic route handling for categories, but I want to make dynamic handling for both models with same pattern. Something like this get '/:page' => 'pages#action' and after that get '/:category' => 'categories#action'
0

You can rename routes as the following:

get 'categories', path_names: { index: 'category' }
get 'pages', path_names: { index: 'page' }

See this link Override some routes

If you want change all routes that called categories to category Try it:

get 'categories', :path => 'category'

See this link Change all resources that called 'categories'

For /science to be show category that is name "science" Try this:

  1. change routes as:

    get 'categories/:name' => 'categories#show'

  2. In action show in categories_controller read category using @category = Category.find_by_name(params[:name])

You can add remaining routes as the following using index_controller:

get '/home' => 'index#action'
get '/about' => 'index#anotheraction'

To change your URL between two controllers Try This:

  1. In pages_controller in action show do this: if Page.where(name: params[:name]).first.present? your code to show this page else render 'categories/show' end

  2. Check this link render to another controller whit another action

1 Comment

I mean that I have same patterns for pages#show and categories#show. For example, if user goes '/test' first route should try to handle it as a page, and if there is no page, 2nd route should try to handle this url as category.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.