Forum: Ruby on Rails How can I link/anchor down the page with #anchor in urls?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Sfdesigner S. (Guest)
on 2006-06-10 07:02
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"</a> through <a name=section6"</a>. 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
Philip R. (Guest)
on 2006-06-10 14:35
(Received via mailing list)
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
Nate C. (Guest)
on 2006-06-10 19:47
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.
This topic is locked and can not be replied to.