I am using Ruby on Rails 3.2 and have created a simple test blog application. There is a Post model and a Comment model where a post has_many :comments and a comment belongs_to :post.
in routes.rb:
resources "posts" do
resources "comments"
end
I display the comments at the bottom of the parent post’s page and submit new comments via AJAX. Therefore, I think it is unnecessary for the user to be able to visit /posts/1/comments/XXX. However, if I remove resources "comments" from my routes then the commenting functionality doesn’t work anymore. How can I prevent the user from visiting /posts/1/comments/XXX in the browser but keep the commenting AJAX functionality working?
What you can do as a solution is condition the route with the request being a xhr request. You can do it the following way:
You can find more info about routing constraints in this blog post.