Skip to content

Instantly share code, notes, and snippets.

@tjarmain
Created August 9, 2012 18:19
Show Gist options
  • Save tjarmain/3306766 to your computer and use it in GitHub Desktop.
Save tjarmain/3306766 to your computer and use it in GitHub Desktop.
<h1>New product</h1>
<% @images.each do |image| %>
<%= image_tag image %>
<% end %>
<%= link_to 'Back', products_path %>
<div id="product-loading">
<h1>We're loading your amazing find...</h1>
<%= image_tag "loading.gif" %>
</div>
require 'mechanize'
require 'open-uri'
class Product < ActiveRecord::Base
attr_accessible :name, :price, :thumbnail_image, :original_image, :source_url
def self.create_from_url(url)
agent = Mechanize.new
agent.user_agent_alias = 'Mac Safari'
page = agent.get(url)
image_urls_array = Array.new
page.images.each { |img| image_urls_array << img.to_s }
return image_urls_array
end
end
class ProductsController < ApplicationController
...
def pre_post
url = params[:url]
images = Product.create_from_url(url)
redirect_to :controller => 'products', :action => 'new', :images => images
end
def new
@product = Product.new
@images = params[:images]
respond_to do |format|
format.html # new.html.erb
format.json { render json: @product }
end
end
...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment