模型中Rails应用程序的基本URL [英] Base URL of Rails app in Model
问题描述
为了能够查看用户是否在Facebook上共享了我的页面,我希望能够创建这种URL:
In order to be able to see if a User has shared my page on Facebook, I want to be able to create this kind of URLs:
http://graph.facebook.com/?id=http://stylehatch.co/some_unique_user_token
其中 http://stylehatch.co 应该是我的基本URL.我正在考虑在用户模型中使用一种方法来构建并返回该URL,但无法从模型中访问root_url.
Where http://stylehatch.co would need to be my base URL. I am thinking on having a method in the User model that will build and return that URL, but I can't access root_url from within my model.
如何从模型获取应用程序的基本URL?
How can I get the base URL of my application from a Model?
如果我的网址看起来像这样:
Say if my urls look like this:
http://myapp.com/users/new
http://myapp.com/users/2/show
如何从模型中获取" http://myapp.com "?我添加了:
How can I get the "http://myapp.com" from my model? I have added:
include Rails.application.routes.url_helpers
在我的模型中,但似乎root_url为nil.有什么想法吗?这是正确的作为Model方法,还是应该将其放在助手中?
in my model, but it seems that root_url is nil. Any thoughts? Is this correct to be a Model method, or should I place it in a helper?
谢谢
推荐答案
您可以使用环境变量:
ROOT_URL=http://myapp.com
应用内
ENV['ROOT_URL']
这篇关于模型中Rails应用程序的基本URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!