问题
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