f1v
f1v

Reputation: 153

modify rails object after save based on some javascript

Im wondering on how to approach this. For simplicity lets say I submit a link from a website through javascript to my rails server. I want my rails server to create a page based off that link and pull out the logo from that page. Right now I have the code to find a logo off a webpage but not sure how I can link the two together.

My controller takes in the json post request and saves a page object but how would I know ask it to run my js and save that image location in the page object as well?

Any help would be great thanks!

Upvotes: 0

Views: 112

Answers (1)

Mik
Mik

Reputation: 4187

If I understand the question: you want to add a page after the server took the logo by following the link? And the page will be created with this logo. If so, then the server itself should follow the link by using for example mechanize, and there find the link to the logo. For example:

require 'mechanize'
...
def create
  @page = Page.new(params[:page])
  link = @page.link
  agent = Mechanize.new
  page = agent.get link
  img_src = page.search("#logo").first.attributes['src']
  @page.logo = img_src
  if @page.save
  ...
end

More complex example: http://caldeas.com/2010/08/01/using-mechanize-to-download-images-from-stock-exchange/

This is probably not what you wanted, but it will solve your problem.

Upvotes: 1

Related Questions