`
johnnyhg
  • 浏览: 343353 次
  • 来自: NA
社区版块
存档分类
最新评论

如何在Rails的controller中取得URL中的anchor值

阅读更多

有人问,如何在Rails的控制器中取得URL中的锚点的值,这个需求比较奇特,以前没搞过,于是google了一把,在“Getting the # value ” 这个帖子看到有人有类似的问题,他说出了在controller中直接取不到的原因是“For normal links/urls the #anchor is a client side feature and is often not even sent to the server as part of a request.”,恩,确实直接取不到,只能使用类似JS的方法来取了,因为其没有将URL全部提交到服务端,所以是无法取值的。

另外,看了下Rails中生成anchor的方法比较简单,如下:

 

Ruby代码
  1. <%= url_for( :action  =>  'play' :anchor  =>  'player' ) %>  
  2. # => /messages/play/#player   
  3.   
  4. <%= url_for(:action  =>  'checkout' :anchor  =>  'tax&ship' ) %>  
  5. # => /testing/jump/#tax&ship   
  6.   
  7. <%= url_for(:action  =>  'checkout' :anchor  =>  'tax&ship' :escape  =>  false ) %>  
  8. # => /testing/jump/#tax&ship   
详细的请参考API:ActionView::Helpers::UrlHelper
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics