简体   繁体   English

link_to中的锚标记

[英]anchor tag in link_to

Found it difficult to frame the question, here's my problem. 发现很难提出问题,这是我的问题。

alert.html alert.html

<div class="alert_text">
 <%= link_to "Comment", :action=>:show, :id=>lp.id %>
</div>

when I click on the link it takes me to show page 当我单击链接时,我需要显示页面

show.html show.html

table is displayed at beginning of the page after that, below part of code. 之后,表格的底部会显示在页面的开头。

 <div id="comments">
  <%= render :partial=>'comments' %>
 </div>

what I need is, when I click on link Comment it must load show page as normal but should direct to comments part of page . 我需要的是,当我单击链接评论时,它必须正常加载显示页面,但应直接指向页面的评论部分

Edit : Just like as it happens in this stackoverflow.com , on top left StackExchange when you click on inbox message. 编辑 :就像发生在此stackoverflow.com中一样 ,当您单击收件箱消息时,位于StackExchange左上方。

It sounds like what you want is the anchor option with a path helper. 听起来您想要的是带有路径帮助器的anchor选项。 Assuming you're using RESTful routing, you should be able to do something like: 假设您使用的是RESTful路由,那么您应该可以执行以下操作:

<%= link_to "Comments", my_model_path(lp, anchor: 'comments') %>

You would need to change my_model to the whatever your resource is called. 您将需要将my_model更改为调用资源的任何名称。 So if you have resources :articles in routes.rb, it would be article_path(lp, anchor: 'comments') 因此,如果您在routes.rb中有resources :articles ,那么它将是article_path(lp, anchor: 'comments')

Give your comment div an ID and use it link this: 给您的评论div一个ID,并使用它链接到此:

<%= link_to "Comment##{lp.id}", :action=>:show %>

If the div comment has your ID, it will straight go to that spot. 如果div注释具有您的ID,它将直接转到该位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM