Rails 中基于位置的 URL 路由

发布于 2024-09-04 01:19:08 字数 440 浏览 6 评论 0原文

我是 Rails 新手。我想了解更多关于基于 IP 的位置、地理位置 URL 路由的信息。

我有 2 个问题。

第一个..

基于国家/地区的位置,如何进行 URL 路由?

如果我的 IP 来自美国,我应该被定向到美国主页。

如果我来自英国,我应该从英国主页定向。

如果我来自任何其他国家,我应该被定向到国际主页。

你能帮我解决rails代码吗?你能帮我提供一个好的教程吗?

现在是第二个问题......

如何使用地理位置将人们路由到他们的城市?

假设我从洛杉矶访问网络,我应该被定向到洛杉矶主页。

如果我在一个小镇访问网络,我应该被定向到最近的城市主页。

如果我在国际位置[美国境外说] 那么我应该被定向到国际页面

期待您的帮助

Gautam

I am new to rails. I want to know more about IP based Location, GeoLocation URL routing.

I have 2 questions.

The first one..

Based on the location of the country, how to you have URL routing?

If my IP is from USA, I should be directed towards USA homepage.

If I am from UK, I should be directed from UK homepage.

If I am from any other country, I should be directed towards International homepage.

Could you please help me with the rails code? Could you help me with a good tutorial?

and now the second question....

How to use GeoLocation to route people to their cities?

Lets say I access the net from LA, I should be directed to LA homepage..

If I am accessing the net at a small town, I should be directed towards closest city homepage..

If I am at an international location [ outside USA say ] then I should be directed towards International page

Looking forward for your help

Gautam

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帝王念 2024-09-11 01:19:08

要根据请求的 IP 估计位置,您应该查看 Geokit

存储位置后,您可以根据他们在会话中的位置简单地渲染您选择的模板。您可以为所有访问者保留相同的 URL 结构,同时根据每个访问者的请求来源呈现特定的模板。

class ApplicationController < ActionController::Base
  geocode_ip_address
end

正如您所看到的,GeoKit 附带了一个漂亮的助手来完成您想要的操作 - 它将位置存储在访问者会话[:geo_location] 变量中。

希望有帮助!

To estimate location based on the IP of the request you should look at Geokit.

Once you have the location stored you could simply render the template of your choosing based off the location they have in their session. You can retain the same URL structure for all visitors while rendering each of them specific templates based on their requests origin.

class ApplicationController < ActionController::Base
  geocode_ip_address
end

As you can see GeoKit comes with a nifty helper for doing just what you want - it will store the location in the visitors session[:geo_location] variable.

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文