有人问,如何在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代码
- <%= url_for( :action => 'play' , :anchor => 'player' ) %>
- # => /messages/play/#player
- <%= url_for(:action => 'checkout' , :anchor => 'tax&ship' ) %>
- # => /testing/jump/#tax&ship
- <%= url_for(:action => 'checkout' , :anchor => 'tax&ship' , :escape => false ) %>
- # => /testing/jump/#tax&ship