0

I'll explain when open www.domain.com/:id should be check the :id is a page then use

get ':id' => 'pages#show'

else if :id is a Category use

get ':category' => 'categories#show'

sorry for my english

get ':page' => 'pages#show'
resources :categories, :path => '/' do
  resources :posts, :path => '/'
end
Paweł Dawczak
  • 9,519
  • 2
  • 24
  • 37
Rasheed
  • 35
  • 1
  • 6

3 Answers3

1

why dont you write something like this:

routes.rb

get '/:category_id/:post_id', to: 'categories#post'

and in your categories_controller.rb

def post
  #params[:category_id] and params[:post_id] will contain the params from the url
end
aelor
  • 10,892
  • 3
  • 32
  • 48
  • this is not the problem for me the problem here get ':page' => 'pages#show' get ':category' => 'categories#show' when going to category route like domain.com/programming it's retrieve the page route – Rasheed Mar 11 '15 at 10:55
  • @Rasheed kya aap hindi me bata sakte hain apni pareshani ko – aelor Mar 11 '15 at 10:58
  • I don't understand, I don't speak this language – Rasheed Mar 11 '15 at 11:04
  • @Rasheed No problem.. I thought you understood hindi, coz it becomes a little difficult sometimes with non-native english speakers to communicate. btw.. I am not able to understand your problem fully .. can you please explain it a bit more – aelor Mar 11 '15 at 11:07
  • ok, I'll explain when open www.domain.com/:id should be check the :id is page then use get ':id' => 'pages#show' else if :id is category use get ':category' => 'categories#show' sorry for my english – Rasheed Mar 11 '15 at 11:21
  • @Rasheed - please update your question with informations you've provided in the comment, so others are able to help you. – Paweł Dawczak Mar 11 '15 at 11:22
1

constraints for routes is way to go, but I would suggest a bit different solution for your problem.

Consider creating custom Constraint objects, which will query the database in order to check if Post or Category exists:

app/lib/post_constraint.rb

class PostConstraint
  def matches?(request)
    Post.exists?(request[:id])
  end
end

app/lib/category_constraint.rb

class CategoryConstraint
  def matches?(request)
    Category.exists?(request[:id])
  end
end

In your app/config/routes.rb

get ":id" => "posts#show",      constraints: PostConstraint.new
get ":id" => "categories#show", constraints: CategoryConstraint.new

You have to be aware of the fact, that id is very poor candidate for such comparison, because the Post is checked in first place, and if there is record matching, the "posts#show" will be accessed, and CategoryConstraint wot be even bothered.

For more, check the documentation.

You should consider adding a slug for both models to easier serve exactly what user expects to see. For this purpose, try gem friendly_id.

Hope that helps! Good luck!

Paweł Dawczak
  • 9,519
  • 2
  • 24
  • 37
  • thank you so much, I have a problem with friendly_id doesn't support arabic characters for this I didn't used – Rasheed Mar 11 '15 at 14:57
  • Oh I see. I'm afraid I can't help much in that matter. You should try to generate slugs on your own (to limit possible collisions between your models of `Post` and `Category`), maybe this [answer](http://stackoverflow.com/a/1302183/4381282) will help you in your efforts of generating mentioned slugs. Good luck! – Paweł Dawczak Mar 11 '15 at 15:08
0

Couple of solutions If your post id and category id follow a pattern you can add constraints to your routes

1)

get ':post_id' => 'posts#show', :constraints => { :id => /[A-Z]\d{5}/}

get ':category_id' => 'categories#show', :constraints => { :id => /[1-9]\d{5}/ }

2) Add a custom method where both post_id/category_id routes point, in that check if the id is a post id or category id and based on that render the page

Abhay Kumar
  • 1,582
  • 1
  • 19
  • 45