Created
August 9, 2012 18:19
-
-
Save tjarmain/3306766 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>New product</h1> | |
<% @images.each do |image| %> | |
<%= image_tag image %> | |
<% end %> | |
<%= link_to 'Back', products_path %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="product-loading"> | |
<h1>We're loading your amazing find...</h1> | |
<%= image_tag "loading.gif" %> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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