如何使用Ruby on Rails 5将URL中的数据提取到表单中 [英] How to pull data from URL into form with Ruby on Rails 5

查看:127
本文介绍了如何使用Ruby on Rails 5将URL中的数据提取到表单中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格,用户可以填写以共享设计作品.但是,字段与dribbble.com非常相似.

I have a form where users can fill to share design work. However fields are pretty similar to dribbble.com .

我看到一些网站允许用户粘贴自动填写表格的链接.可以在 https://www.uplabs.com/submit

I see some websites allow user to paste a link that auto fills the form. Example can be seen at https://www.uplabs.com/submit

我不需要有关执行此操作的确切代码.您能告诉我一个方向吗?一些插件,一些示例,...?

I dont need the exact codes on how to do this. Can you please advise a direction? some plugins, some examples, ...?

示例;链接

谢谢!

推荐答案

您可以使用对Rails控制器的Ajax调用来做到这一点. 创建一个Rails控制器,该控制器将从网页标题和元描述中删除.您可以像下面的示例一样使用机械化.

You can do that using using Ajax call to a Rails controller. Create a rails controller which will scrap from the webpage title and meta description. You can use mechanize like in the example below.

然后使用Ajax调用将粘贴的url发送到该rails控制器操作,然后执行抓取操作

Then using Ajax call send pasted url to that rails controller action and there perform scrapping

$("#url_input").on("paste", function(e) {
  $.ajax({
    url: "/url_data_grabber/grab",
    data: $("#url_input").val(),
    success: function(result) {
      $("#title").val(result["title"]);
      $("#title").val(result["description"]);
    }
  });
});




require 'mechanize'
class UrlGrabberController < ApplicationController
  def grab
    pasted_url = params.fetch("pasted_url")
    page = agent.get(pasted_url)
    title = page.title

    node = page.at("head meta[name='description']")
    description = node["content"]

    render json: {title: title, description: description}
  end
end

这篇关于如何使用Ruby on Rails 5将URL中的数据提取到表单中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆