How can I link/anchor down the page with #anchor in urls?


#1

I’m trying to expand on a blog tutorial and one thing I can’t figure out
is how to use anchors within pages.

For example say I have sections on a page with anchors like <a
name=section1" through <a name=section6". Using older non-rails
methods I would construct urls that contains the pound symbol and anchor
name in the url such as http://www.foobar.com/page.html#section1.
Another practice I use to use is appending just the pound sign at the
end of the url to jump to the top of the current page.

I’ve been able to append name/value pairs to the end of urls using
hashes within link_to statements. It seems the necessary question mark
is added automagically before the name/value pair get added. I’m not
sure how to add on to the url without the question mark being added.

Has anyone figured out a way to do that? I know there are a few other
ways I could present the content using different approaches but I would
like to figure out how to anchor down the pages if possible.

Thanks,
DAN


#2

sfdesigner sfdesigner wrote:

Has anyone figured out a way to do that? I know there are a few other
ways I could present the content using different approaches but I would
like to figure out how to anchor down the pages if possible.

You can use :anchor with link_to, e.g.

link_to “Comments”,
:controller => “post”,
:action => “view”,
:id => post.id,
:anchor => “comments”

will generate something like:

/post/view/1#comments


Philip R.
http://tzinfo.rubyforge.org/ – DST-aware timezone library for Ruby


#3

I’ve been trying to figure this out as well so i hope you (Dan) don’t
mind if i jump in here also and ask a question.

Is there a way to add the anchor from within the controller action that
is rendering the page?
basically, i have 3 forms on a page and if the second form does not
validate i want to add an anchor to the page that references the second
form so it will automatically scroll down to the errors.