views:

89

answers:

2

given a blog style application:

#models
class Post < ActiveRecord::Base
  has_many :comments
end

class Comment < ActiveRecord::Base
  belongs_to :post
end

#routes.rb
map.resources :posts do |posts|
  posts.resources :comments
end

how do I generate routes to an id on a page? Examples

/posts/1#comments
/posts/2#comment14
+1  A: 

I don't think the routes generate methods for anchors like that, but you can add anchors into the url generators for posts.

 post_path(@post, :anchor => "comments")
 post_path(@post, :anchor => "comment#{@comment_id}")
erik
A: 

The way I handled this was to generate the path to the comment show action which then redirected to the anchor via the method erik posted.

guitsaru