与Rails一起使用 [英] Using embedly with Rails
问题描述
我是Rails的新手,正尝试嵌入使用.有没有逐步讲解的教程?我肯定这不是很困难,因为我只是想尝试一下,所以我只需要很少的手.任何帮助,我们将不胜感激!
I am extremely new to rails and am trying to use embedly. Is there a tutorial that walks through it step by step? Im sure its not very difficult I just need to little of hand holding since I am just trying to get into this. Any help is greatly appreciated!
谢谢.
推荐答案
我不确定您要如何使用它.
无论如何,这都可以帮助您入门.
让我知道你是否被卡在某个地方.
I am not sure how you want to use it.
In any case this could help you get started.
Let me know if you get stuck somewhere.
-
您必须先添加宝石并捆绑
gem'embedly','〜> 1.9.1'(当前版本)
You have to add the gem first and bundle
gem 'embedly', '~> 1.9.1' (what ever current version)
以下代码声明了一个名为display的方法,您可以在视图中使用该方法.
笔记
*您必须注册才能嵌入并获取API密钥.
*我建议您隐藏您的秘密密钥.
*您可以将以下代码放入模型文件或其他文件中
放置,但为了简单起见,将代码放在app/helpers文件夹下
The following code declares a method called display which you can use in your views.
Notes
*You have to sign up to embedly and get an API key.
*I recommend you hide your secret key.
*You could put the following code in a model file or probably other
places but for the sake of simplicity put the code under the app/helpers folder
将以下代码放入: app/helpers/application_helper.rb
Put the following code in: app/helpers/application_helper.rb
require 'embedly'
require 'json'
def display(url)
embedly_api = Embedly::API.new(key: THIS IS WHERE YOUR API KEY GOES)
obj = embedly_api.oembed :url => url
(obj.first.html).html_safe
end
- 将此添加到您的视图中:
例如app/views/welcome/index.html.erb
- Put this in your views:
for example app/views/welcome/index.html.erb
<%= display("http://vimeo.com/18150336") %>
转到: http://embed.ly/docs/explore/oembed?url=http%3A%2F%2Fvimeo.com%2F18150336
在这种情况下,它将显示您可以从该特定URL检索的嵌入内容" http://vimeo.com/18150336 ".
It will show you what embedly can retrieve from that particular url in this case "http://vimeo.com/18150336".
如果要检索标题,请更改:
(obj.first.html).html_safe to (obj.first.title).html_safe
If you want to retrieve the title, change :
(obj.first.html).html_safe to (obj.first.title).html_safe
如果要检索缩略图,请更改:
(obj.first.html).html_safe to (obj.first.thumbnail_url).html_safe
If you want to retrieve the thumbnail, change:
(obj.first.html).html_safe to (obj.first.thumbnail_url).html_safe
如果要检索图标,请更改:
(obj.first.html).html_safe to (obj.first.favicon_url).html_safe
If you want to retrieve the favicon, change :
(obj.first.html).html_safe to (obj.first.favicon_url).html_safe
如果要检索描述,请更改:
(obj.first.html).html_safe to (obj.first.description).html_safe
If you want to retrieve the description, change :
(obj.first.html).html_safe to (obj.first.description).html_safe
请注意,代码如何包装在().html_safe
中,您也可以执行raw.()
如果您不这样做,则嵌入式代码将显示为字符串.
Note how code is wrapped in ().html_safe
you could also do raw.()
If you don't do this the embedded code will be shown as a string.
这篇关于与Rails一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!