1

我对 Rails 非常陌生,正在尝试使用嵌入。有没有一步一步的教程?我确定这不是很困难,我只需要很少的手,因为我只是想进入这个。任何帮助是极大的赞赏!

谢谢。

4

2 回答 2

2

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.

  1. You have to add the gem first and bundle
    gem 'embedly', '~> 1.9.1' (what ever current version)

  2. 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

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
  1. Put this in your views:
    for example app/views/welcome/index.html.erb

<%= display("http://vimeo.com/18150336") %>

Go to: http://embed.ly/docs/explore/oembed?url=http%3A%2F%2Fvimeo.com%2F18150336

It will show you what embedly can retrieve from that particular url in this case "http://vimeo.com/18150336".

If you want to retrieve the title, change :
(obj.first.html).html_safe to (obj.first.title).html_safe

If you want to retrieve the thumbnail, change:
(obj.first.html).html_safe to (obj.first.thumbnail_url).html_safe

If you want to retrieve the favicon, change :
(obj.first.html).html_safe to (obj.first.favicon_url).html_safe

If you want to retrieve the description, change :
(obj.first.html).html_safe to (obj.first.description).html_safe

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.

于 2015-03-06T21:44:02.917 回答
1

Have a look at the following URLS, it should make your job easier.

https://github.com/embedly/embedly-ruby

http://blog.enbake.com/embedly-integration-in-rails/

于 2015-02-20T07:23:56.943 回答