Created
September 30, 2014 11:11
-
-
Save mdunbavan/2c8915214469ea8d2e71 to your computer and use it in GitHub Desktop.
book gallery
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 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 |
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
| <%= f.simple_fields_for :gallery do |builder| %> | |
| <%= builder.input :name %> | |
| <%= render 'galleries/form', :f => builder %> | |
| <% 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
| <div class="inputs"> | |
| <h3>Create image gallery here, add multiple images one after the other.</h3> | |
| <%= f.simple_fields_for :images, :wrapper => 'inline' do |builder| %> | |
| <%= render 'image_fields', :f => builder %> | |
| <%= link_to_add_association 'add image', f, :images, :render_options => {:wrapper => 'inline' }, :class => 'fa fa-plus' %> | |
| <% end %> | |
| </div> | |
| # Quick breakdown here: the images are added into the gallery via 'cocoon' gem which assigns them as an array in images_attributes | |
| # as you will see beolow in the params |
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
| #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 |
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
| "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