Skip to content

Instantly share code, notes, and snippets.

@vladimir-vg
Created May 15, 2011 23:03
Show Gist options
  • Save vladimir-vg/973651 to your computer and use it in GitHub Desktop.
Save vladimir-vg/973651 to your computer and use it in GitHub Desktop.
config.grid_fs_host = Mongoid.config.master.connection.host
config.grid_fs_host = Mongoid.database.connection.primary_pool.host
class Image
include Mongoid::Document
embedded_in :market_item
mount_uploader :attach, ImageUploader
end
class MarketItem
include Mongoid::Document
embeds_many :images, :stored_as => :array
# BUGFIX
# next code-line needed for fix bug array described here:
# http://tinyurl.com/6yntj3r
accepts_nested_attributes_for :images
end
<%= form_for @market_item, :html => {:multipart => true} do |f| -%>
<%= f.fields_for :images do |builder| -%>
<%= builder.label :attach %>
<%= builder.file_field 'attach' %>
<% end -%>
<%= f.submit "Отправить" %>
<% end -%>
# GET /market_items/new
# GET /market_items/new.xml
def new
@market_item = MarketItem.new
3.times { @market_item.images.build }
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @market_item }
end
end
# POST /market_items
# POST /market_items.xml
def create
@market_item = MarketItem.new(params[:market_item])
respond_to do |format|
if @market_item.save
# BUGFIX
# Don't know why, but nested images does not saves automaticly
# bug a bit described here (at end):
# http://flux88.com/blog/using-carrierwave-with-mongoid/
@market_item.images.each {|x| x.save!}
format.html { redirect_to(@market_item,
:notice => 'created.') }
format.xml { render :xml => @market_item,
:status => :created,
:location => @market_item }
else
format.html { render :action => "new" }
format.xml { render :xml => @market_item.errors,
:status => :unprocessable_entity }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment