在 Rails 应用程序中设置 facebook canvas url 和路线的正确方法

发布于 2024-08-22 04:31:18 字数 1932 浏览 11 评论 0原文

我有一个现有的 Rails 应用程序,我正在向其中添加一个新控制器来处理来自我正在构建的小型 facebook 应用程序(使用 facebooker 插件)的请求。

在 facebook 应用程序的配置中,我将画布回调 url 设置为 http://my.host.ip/fb/< /a>

在纯 Facebook 应用程序中,URL 将省略 /fb/,用户将被定向到应用程序的主页。但由于这是一个现有的应用程序,我不能这样做。

现在在我的routes.rb中,我有:

map.connect '/fb/like/:id', :controller => 'facebook_app', :action => "like"
map.connect '/fb/:category', :controller => 'facebook_app', :action => "index", :category => "default"

Ao 当用户访问 http://apps.facebook.com/my_app_name / facebook 调用 http://my.host.ip/fb/ 渲染效果很好。

在该页面上,我有指向“喜欢”操作的链接:

<%= link_to "like", :controller => "fb", :action => "like", :id => id %>

唯一的问题是这些内容呈现为:

http://apps.facebook.com/my_app_name/fb/like/12345

当我想要的是:

http://apps.facebook.com/my_app_name/like/12345

看看/fb/如何让我悲伤?

有办法解决这个问题吗?或者在路由定义中以某种方式表明 /fb/ 仅用于传入 URL?或者在 url 生成器中以某种方式省略 /fb/?

我找到了一个解决方法......如果我将此路由添加到现有的“like”路由之上:

map.connect '/like/:id', :controller => 'facebook_app', :action => "like"

那么第一个路由就是 link_to url 生成器使用的路由,并生成正确的 URL:

http://apps.facebook.com/my_app_name/like/12345

点击后,Facebook 会向我的应用程序发出此请求:

http://my.host.ip/fb/like/12345

与原来匹配“喜欢”路线。

不过,我不想对 Facebook 控制器中的每个操作都执行此操作。

I have an existing rails application to which i am adding a new controller to handle requests from a small facebook app (using the facebooker plugin) i am building.

In the facebook app's config i set the canvas callback url to http://my.host.ip/fb/

In a pure facebook application the url would leave off the /fb/ and the user would be directed to the app's home page. but since this is an existing application, i cannot do that.

Now in my routes.rb i have:

map.connect '/fb/like/:id', :controller => 'facebook_app', :action => "like"
map.connect '/fb/:category', :controller => 'facebook_app', :action => "index", :category => "default"

Ao when the user visits http://apps.facebook.com/my_app_name/ facebook makes a call to http://my.host.ip/fb/ and that renders fine.

On that page i have links to the "like" action:

<%= link_to "like", :controller => "fb", :action => "like", :id => id %>

Only problem is these get rendered as:

http://apps.facebook.com/my_app_name/fb/like/12345

When what i want is:

http://apps.facebook.com/my_app_name/like/12345

See how the /fb/ is causing me grief?

Is there a way around this? either some way in the routes definition to say that the /fb/ was only for incoming URLs? or some way in the url generator to omit the /fb/?

I found one workaround… if I add this route above the existing "like" route:

map.connect '/like/:id', :controller => 'facebook_app', :action => "like"

Then that first route is what's used by the link_to url generator and the correct URL gets generated:

http://apps.facebook.com/my_app_name/like/12345

Which when clicked causes facebook to make this request to my app:

http://my.host.ip/fb/like/12345

Which is matched by the original "like" route.

I'd rather not have to do this for every action in my facebook controller though.

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

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

发布评论

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

评论(2

套路撩心 2024-08-29 04:31:18

您可以尝试将您的callback_url设置为http://my.host.ip/。在此操作中,您可以使用 request_comes_from_facebook? 方法检测请求是否来自 Facebook,如果是,则将其重定向到您的 Facebook 控制器。

然后你的后续链接就可以工作了。

You could maybe try to set your callback_url to just http://my.host.ip/. In that action, you can then detect if the request comes from Facebook with request_comes_from_facebook? method and redirect it to your facebook controller if it`s the case.

Then your subsequent links would work.

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