简体   繁体   English

@rails_folks | ” http://twitter.com/#!/user/followers” |请解释

[英]@rails_folks | ''http://twitter.com/#!/user/followers" | Please explain

How would you achieve this route in rails 3 and the last stable version 2.3.9 or soish? 您将如何在Rails 3和最新的稳定版本2.3.9或soish中实现此路线?

Explained 解释

I don't really care about the followers action. 我不太在乎追随者的行为。 What I'm really after is how to create '!#' in the routing. 我真正想要的是如何在路由中创建“!#”。

Also, What's the point of this. 另外,这有什么意义。 Is it syntax or semantics? 是语法还是语义?

Rails doesnt directly get anything after the # . #之后,Rails不会直接得到任何东西。 Instead the index page checks that value with javascript and makes an AJAX request to the server based on the url after the # . 相反,索引页使用javascript检查该值,并根据#之后的网址向服务器发出AJAX请求。 What routes they use internally to handle that AJAX request I am not sure. 我不确定他们在内部使用什么路由来处理该AJAX请求。

The point is to have a Javascript powered interface, where everyone is on the same "page" but the data in the hashtag allows it to load any custom data on the fly, and without loading a whole new page if you decide to view a different user, for instance. 关键是要有一个由Javascript驱动的界面,每个人都在同一个“页面”上,但是#标签中的数据允许它动态加载任何自定义数据,并且如果您决定查看其他页面,则无需加载整个新页面。用户。

The hash part is never sent to the URL, but it is a common practice to manipulate the hash to maintain history, and bookmarking for AJAX applications. 哈希部分永远不会发送到URL,但是通常的做法是操纵哈希值以维护历史记录,并为AJAX应用程序添加书签。 The only problem being that by using a hash to avoid page reloads, search engines are left behind. 唯一的问题是,通过使用散列来避免页面重新加载,搜索引擎就被抛在了后面。

If you had a site with some links, 如果您的网站包含一些链接,

http://example.com/#home
http://example.com/#movies
http://example.com/#songs

Your AJAXy JavaScript application sees the #home , #movies , and #songs , and knows what kind of data it must load from the server and everything works fine. 您的AJAXy JavaScript应用程序可以看到#home#movies#songs ,并且知道必须从服务器加载哪种数据,并且一切正常。

However, when a search engine tries to open the same URL, the hash is discarded, and it always sends them to http://example.com/ . 但是,当搜索引擎尝试打开相同的URL时,哈希将被丢弃,并且始终将其发送到http://example.com/ As a result the inner pages of your site - home, movies, and songs never get indexed because there was no way to get to them until now. 结果,您的站点的内部页面(家庭,电影和歌曲)将永远不会被索引,因为到目前为止无法访问它们。

Google has creating an AJAX crawling specification or more like a contract that allows sites to take full advantage of AJAX while still reaping the benefits of indexing by searching engines. Google已创建了一个AJAX爬网规范,或更像一个合同,该合同允许网站充分利用AJAX的优势,同时仍可通过搜索引擎获得索引的好处。 You can read the spec if you want, but the jist of it is a translation process of taking everything that appears after #! 您可以根据需要阅读规范,但其本质是翻译过程,它提取#!之后出现的所有内容#! and adding it as a querystring parameter. 并将其添加为querystring参数。

So if your AJAX links were using #! 因此,如果您的AJAX链接使用#! , then a search engine would translate a URL like, ,那么搜索引擎会翻译如下网址,

http://example.com/#!movies

to

http://example.com/?_escaped_fragment_=movies

Your server is supposed to look at this _escaped_fragment_ parameter and respond the same way that your AJAX does. 您的服务器应该查看此_escaped_fragment_参数,并以与AJAX相同的方式进行响应。

Note that HTML5's History interface now provides methods to change the address bar path without needing to rely upon the hash fragment to avoid page reloads. 请注意,HTML5的“ 历史记录”界面现在提供了更改地址栏路径的方法,而无需依赖散列片段来避免页面重新加载。

Using the pushState and popState methods 使用pushStatepopState方法

history.pushState(null, "Movies page", "/movies");

you could directly change the URL to http://example.com/movies without causing a page refresh. 您可以直接将URL更改为http://example.com/movies而无需刷新页面。 Search engines can continue to use the same URL that you would be using in that case. 搜索引擎可以继续使用您在这种情况下将使用的相同URL。

The part after the # in a URI is called the fragment identifier , and it is interpreted by the client, not the server. URI中#后面的部分称为片段标识符 ,由客户端而不是服务器解释。 You cannot route this, because it will never leave the browser. 您无法路由此路由,因为它将永远不会离开浏览器。

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

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