使用者可以填入網址,網站自動抓標題和簡介 http://embed.ly/docs/tutorials/jquery_preview
參考解法
Xdite 解法
由後端抓
1. 安裝 embedly https://github.com/embedly/embedly-ruby
gem install embedly
產生時要先有url , 後續再修改model
def create
@link = current_user.links.build(link_params)
@link.save
redirect_to root_path
end
private
def link_params
params.require(:link).permit(:url,:user_id)
end
3. 修改link model
用after_create , 再來update由embedly抓回來的資料
用after_create , 再來update由embedly抓回來的資料
class Link < ActiveRecord::Base belongs_to :user after_create :update_from_embedly def update_from_embedly link = self embedly_api = Embedly::API.new(:key => Setting.embedly_key) embedly_objs = embedly_api.oembed :url => link.url embedly_obj = embedly_objs[0] response_data = embedly_obj.marshal_dump link.title = response_data[:title] link.link_type = response_data[:type] link.provider_name = response_data[:provider_name] link.provider_url = response_data[:provider_url] link.description = response_data[:description] link.thumbnail_url = response_data[:thumbnail_url] link.thumbnail_width = response_data[:thumbnail_width] link.thumbnail_height = response_data[:thumbnail_height] link.save end end
沒有留言:
張貼留言