Last active
August 29, 2015 14:05
-
-
Save aclel/3557ee0d826133a3173b to your computer and use it in GitHub Desktop.
Rails Nested s3_file_field Form
This file contains hidden or 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
# Should a new Product with multiple images | |
# Currently only one of the images is being uploaded into the database, but all images are being uploaded to S3 | |
<%= form_for @product, :html => {:class => 'expanding', multipart: true} do |f| %> | |
<%= f.text_area :name %> | |
<%= f.fields_for :images, Image.new do |im| %> | |
<%= im.s3_file_field :image, :multiple => true, :class => 'js-s3_file_field' %> | |
<%= im.hidden_field :id %> | |
<%= im.hidden_field :imageable_type %> | |
<%= im.hidden_field :imageable_id %> | |
<% end %> | |
<%= f.submit "Post", class: "btn actions post" %> | |
<% end %> |
This file contains hidden or 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
var ready; | |
ready = function() { | |
return $(".js-s3_file_field").each(function() { | |
var $parent = $(this).parent(); | |
return $(this).S3FileField({ | |
done: function(e, data) { | |
var hidden_field = '<input id="product_images_attributes_0_image_url" name="product[images_attributes][0][image_url]" type="hidden" ' + 'val=' + data.result.url +'>'; | |
$parent.append(hidden_field); | |
return $parent.append("<span>" + data.result.url + "</span>"); | |
}, | |
fail: function(e, data) { | |
return alert(data.failReason); | |
}, | |
progress: function(e, data) { | |
window.document.title = parseInt(data.loaded / data.total * 100, 10) + '%' | |
} | |
}); | |
}); | |
}; |
This file contains hidden or 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
# This is the object which needs to have many images | |
class Product < Item | |
has_many :comments, as: :commentable | |
has_many :likes, as: :likeable | |
has_many :images, as: :imageable | |
accepts_nested_attributes_for :images | |
validates :price, presence: true, :numericality => { greater_than_or_equal_to: 0, less_than_or_equal_to: 1000 } | |
end | |
This file contains hidden or 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 new | |
@product = Product.new | |
@product.images.build | |
end | |
def create | |
@product = current_user.products.build(product_params) | |
if @product.save! | |
flash[:success] = "Product created!" | |
redirect_to root_path | |
else | |
@feed_items = [] | |
render :new | |
end | |
end | |
def show | |
end | |
private | |
def product_params | |
params.require(:product).permit(:name, :description, :price, :type, :group_id, images_attributes: [:id, :imageable_type, :imageable_id, :image_url]) | |
end | |
end |
This file contains hidden or 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
# I've stripped out the unnecessary parts of this file. | |
# Note that the Product model is part of the Items table with Single Table Inheritance. | |
# A Product needs to be able to have multiple images | |
ActiveRecord::Schema.define(version: 20140818030750) do | |
create_table "images", force: true do |t| | |
t.string "image_url" | |
t.string "imageable_type" | |
t.integer "imageable_id" | |
t.datetime "created_at" | |
t.datetime "updated_at" | |
end | |
create_table "items", force: true do |t| | |
t.string "name" | |
t.string "description" | |
t.decimal "price" | |
t.integer "status" | |
t.integer "user_id" | |
t.integer "group_id" | |
t.datetime "created_at" | |
t.datetime "updated_at" | |
t.string "type" | |
t.string "image" | |
end | |
create_table "users", force: true do |t| | |
t.string "email", default: "", null: false | |
t.string "encrypted_password", default: "", null: false | |
t.string "reset_password_token" | |
t.datetime "reset_password_sent_at" | |
t.datetime "remember_created_at" | |
t.integer "sign_in_count", default: 0, null: false | |
t.datetime "current_sign_in_at" | |
t.datetime "last_sign_in_at" | |
t.string "current_sign_in_ip" | |
t.string "last_sign_in_ip" | |
t.string "confirmation_token" | |
t.datetime "confirmed_at" | |
t.datetime "confirmation_sent_at" | |
t.string "unconfirmed_email" | |
t.datetime "created_at" | |
t.datetime "updated_at" | |
t.string "username" | |
t.string "country_code" | |
t.string "subregion_code" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment