# Problem in Restful Routing

**URL:** <https://www.ruby-forum.com/t/problem-in-restful-routing/150792>\
**Category:** Rails\
**Created:** [October 13, 2008, 11:32am UTC](https://www.ruby-forum.com/t/problem-in-restful-routing/150792 "2008-10-13T11:32:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rocknrollcar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f14d63/32.png) [@rocknrollcar](https://www.ruby-forum.com/u/rocknrollcar)\
**Post date:** [October 13, 2008, 11:32am UTC](https://www.ruby-forum.com/t/problem-in-restful-routing/150792/1 "2008-10-13T11:32:29Z")

</div>

Hi. I have a problem in restful routing. My project is a discussion  
forum.

The route is

map.resources :forums do |forum|  
forum.resources :topics do |topic|  
topic.resources :posts  
end  
end

Everything in the code I wrote are working good.

An example url for a show action of a forum post would be

[http://localhost:3000/forums/3/topics/4/posts/90](http://localhost:3000/forums/3/topics/4/posts/90)

Now if i replace 90 by 200, I get the post with id 200 which doesn’t  
belong to topic id 4.

How can i prevent this?

Thank you

---

<div class="post-metadata">

**Author:** ![rocknrollcar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f14d63/32.png) [@rocknrollcar](https://www.ruby-forum.com/u/rocknrollcar)\
**Post date:** [October 13, 2008, 12:15pm UTC](https://www.ruby-forum.com/t/problem-in-restful-routing/150792/2 "2008-10-13T12:15:51Z")

</div>

Thanks a lot. I will try this out now

---

<div class="post-metadata">

**Author:** ![rocknrollcar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f14d63/32.png) [@rocknrollcar](https://www.ruby-forum.com/u/rocknrollcar)\
**Post date:** [October 13, 2008, 12:13pm UTC](https://www.ruby-forum.com/t/problem-in-restful-routing/150792/3 "2008-10-13T12:13:55Z")

</div>

I would think that the find code in your PostsController is something  
like

@post = Post.find(params[:id])

This of course directly gets the Post with the id specified in the URL.

What you can do to get around it is to drilldown from the Forum, Topic  
and then Post. Something like this:

@forum = Forum.find(params[:forum\_id], :include =\> { :topics =\> :posts }  
)  
@topic = @forum.topics.select { |topic| topic.id ==  
params[:topic\_id].to\_i }.first  
@post = @topic.posts.select { |post| post.id == params[:id].to\_i }.first

Please note that the code above is not robust as it does not check for  
nils.  
If @topic were nil for example, the last line to get the post will bomb  
out.

/franz

---

<div class="post-metadata">

**Author:** ![rocknrollcar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f14d63/32.png) [@rocknrollcar](https://www.ruby-forum.com/u/rocknrollcar)\
**Post date:** [October 13, 2008, 11:09pm UTC](https://www.ruby-forum.com/t/problem-in-restful-routing/150792/4 "2008-10-13T23:09:02Z")

</div>

I am not sure if this is correct approach but you could apply the  
before filter that could check if given post belongs to given topic  
(and the topic belongs to given forum 🙂 )

On Oct 13, 11:32 am, Rock R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
