How to pull data from URL into form with Ruby on Rails 5

耗尽温柔 提交于 2019-12-11 16:25:56

问题


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

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, ...?

Example; filling form (image, title, designer id, description, etc.) from link a link like https://dribbble.com/shots/1902343-Subscribe-to-our-newsletters

Thank you!


回答1:


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.

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


来源:https://stackoverflow.com/questions/47350502/how-to-pull-data-from-url-into-form-with-ruby-on-rails-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!