Skip to content

Instantly share code, notes, and snippets.

@mdunbavan
Created September 30, 2014 11:11
Show Gist options
  • Select an option

  • Save mdunbavan/2c8915214469ea8d2e71 to your computer and use it in GitHub Desktop.

Select an option

Save mdunbavan/2c8915214469ea8d2e71 to your computer and use it in GitHub Desktop.
book gallery
class BooksController < ApplicationController
def show
@gallery = @book.gallery
end
def new
@book = Book.new
@author = Author.new
@school = School.new
@book.build_author
@gallery = @book.build_gallery
@gallery.images.build
@book.schools.build
end
def create
@book = Book.create(book_params)
end
private
def book_params
params.require(:book).permit(:title, :synopsis, :body, :age, :publisher, :jacket_cover, :story_id, :author_id, :gallery_id, :school, schools_attributes: [:id, :school, :address, :school_name, :website, :_destroy], author_attributes: [:name,:biography], gallery_attributes: [:name, :book_id, images: [:file] ] )
end
end
<%= f.simple_fields_for :gallery do |builder| %>
<%= builder.input :name %>
<%= render 'galleries/form', :f => builder %>
<% end %>
#book.rb
class Book < ActiveRecord::Base
belongs_to :author
belongs_to :gallery
has_many :stories
has_many :images
accepts_nested_attributes_for :gallery
accepts_nested_attributes_for :images
end
#gallery.rb
class Gallery < ActiveRecord::Base
belongs_to :books
has_many :images
accepts_nested_attributes_for :books
accepts_nested_attributes_for :images, :allow_destroy => true
end
#image.rb
class Image < ActiveRecord::Base
belongs_to :gallery
has_attached_file :file, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :file, :content_type => /\Aimage\/.*\Z/
end
"gallery_attributes"=>{
"name"=>"a gallery",
"images_attributes"=>{
"1412075384129"=>{
"file"=>#<ActionDispatch::Http::UploadedFile:0x00000105a92670 @tempfile=#<File:/var/folders/mf/srx7jt8s2rdg0mn5hr98cvz80000gn/T/RackMultipart20140930-38960-71z5zd>, @original_filename="BmYbsf3CYAAxVJG.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"book[gallery_attributes][images_attributes][1412075384129][file]\"; filename=\"BmYbsf3CYAAxVJG.jpg\"\r\nContent-Type: image/jpeg\r\n">
},
"0"=>{"file"=>#<ActionDispatch::Http::UploadedFile:0x00000105a91fb8 @tempfile=#<File:/var/folders/mf/srx7jt8s2rdg0mn5hr98cvz80000gn/T/RackMultipart20140930-38960-1gel0jz>, @original_filename="46246205_1caa2bcb16_o.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"book[gallery_attributes][images_attributes][0][file]\"; filename=\"46246205_1caa2bcb16_o.jpg\"\r\nContent-Type: image/jpeg\r\n">
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment