-
-
Save jgwhite/0d27bcd7b3c3a3335e2d to your computer and use it in GitHub Desktop.
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
# form for books#create page | |
<%= simple_form_for(@book, :html => { :multipart => true }) do |f| %> | |
<%= f.error_notification %> | |
<div class="inputs"> | |
<%= f.file_field :jacket_cover %> | |
<%= f.input :title %> | |
<%= f.input :synopsis, :as => :ckeditor, :label => false, :input_html => { :ckeditor => { :toolbar => 'Full', :height => 400 } } %> | |
<%= f.input :body, :as => :ckeditor, :label => false, :input_html => { :ckeditor => { :toolbar => 'Full', :height => 400 } } %> | |
<%= f.input :age %> | |
<%= f.input :publisher %> | |
</div> | |
<%= f.simple_fields_for :author do |a| %> | |
<%= a.input :name %> | |
<%= a.input :biography, :as => :ckeditor, :label => false, :input_html => { :ckeditor => { :toolbar => 'Full', :height => 400 } } %> | |
<% end %> | |
<%= f.simple_fields_for :gallery do |builder| %> | |
<%= render 'galleries/form', :f => builder %> | |
<% end %> | |
<div class="actions"> | |
<%= f.button :submit %> | |
</div> | |
<% 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
# form for galleries nested | |
<div class="inputs"> | |
<%#= f.simple_fields_for :image do |a| %> | |
<%#= f.input :book_id %> | |
<%#= a.input_field :file, as: :file, multiple: true, name: 'image[file]' %> | |
<%# end %> | |
<%= f.simple_fields_for :images do |builder| %> | |
<%= render 'images/form', :f => builder %> | |
<% end %> | |
</div> |
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
# form for images nested in galleries form | |
<div class="inputs"> | |
<%#= f.input :file %> | |
<%#= f.input_field :file, as: :file, multiple: true, name: 'image[file]' %> | |
<%= f.file_field :file, multiple: true %> | |
</div> |
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 will allow me to create a book it has one gallery | |
class Book < ActiveRecord::Base | |
has_attached_file :jacket_cover, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png" | |
validates_attachment_content_type :jacket_cover, :content_type => /\Aimage\/.*\Z/ | |
validates :jacket_cover, :title, :slug, :synopsis, :body, :age, :publisher, presence: true | |
validates_uniqueness_of :title | |
extend FriendlyId | |
friendly_id :title, use: [:slugged, :finders] | |
belongs_to :author | |
has_one :gallery | |
accepts_nested_attributes_for :author | |
accepts_nested_attributes_for :gallery | |
scope :available, ->{ where(available: true) } | |
scope :unavailable, ->{ where(available: [nil, false]) } | |
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 BooksController < ApplicationController | |
before_action :set_book, only: [:show, :edit, :update, :destroy] | |
before_filter :authenticate_user!, only: [:new, :edit, :update, :destroy] | |
def index | |
@books = Book.order('created_at DESC').all | |
end | |
# GET /books/1 | |
# GET /books/1.json | |
def show | |
@book = Book.friendly.find(params[:id]) | |
@gallery = @book.gallery | |
end | |
# GET /books/new | |
def new | |
@book = Book.new | |
@book.build_author | |
@gallery = @book.build_gallery | |
@gallery.images.build | |
end | |
def create | |
#raise params.inspect | |
@book = Book.new(book_params) | |
#binding.pry | |
respond_to do |format| | |
if @book.save | |
format.html { redirect_to @book, notice: 'Book was successfully created.' } | |
format.json { render action: 'show', status: :created, location: @book } | |
else | |
format.html { render action: 'new' } | |
format.json { render json: @book.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
def update | |
respond_to do |format| | |
if @book.update(book_params) | |
format.html { redirect_to @book, notice: 'Book was successfully updated.' } | |
format.json { head :no_content } | |
else | |
format.html { render action: 'edit' } | |
format.json { render json: @book.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
def destroy | |
@book.destroy | |
respond_to do |format| | |
format.html { redirect_to books_url } | |
format.json { head :no_content } | |
end | |
end | |
private | |
# Use callbacks to share common setup or constraints between actions. | |
def set_book | |
@book = Book.friendly.find(params[:id]) | |
end | |
def book_params | |
params.require(:book).permit(:title, :synopsis, :body, :age, :publisher, :jacket_cover, author_attributes: [:name,:biography], gallery_attributes: [:name, :book_id, images_attributes:[] ] ) | |
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
class GalleriesController < ApplicationController | |
before_action :set_gallery, only: [:show, :edit, :update, :destroy] | |
def index | |
@galleries = Gallery.all | |
end | |
def show | |
@gallery = Gallery.find(params[:id]) | |
@images = @gallery.images | |
end | |
def new | |
@gallery = Gallery.new | |
#@image = @gallery.build_book | |
#@gallery.images.build | |
end | |
def edit | |
@gallery.images.build | |
end | |
def create | |
@gallery = Gallery.new(gallery_params) | |
respond_to do |format| | |
if @gallery.save | |
format.html { redirect_to @gallery, notice: 'Gallery was successfully created.' } | |
format.json { render action: 'show', status: :created, location: @gallery } | |
else | |
format.html { render action: 'new' } | |
format.json { render json: @gallery.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
def destroy | |
@gallery.destroy | |
respond_to do |format| | |
format.html { redirect_to galleries_url } | |
format.json { head :no_content } | |
end | |
end | |
private | |
def set_gallery | |
@gallery = Gallery.find(params[:id]) | |
end | |
def gallery_params | |
params.require(:gallery).permit(:name, :book_id, :images[], images_attributes: [:id, :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
# This is where the gallery data is handled and it has many images | |
class Gallery < ActiveRecord::Base | |
belongs_to :books | |
has_many :images | |
accepts_nested_attributes_for :books | |
accepts_nested_attributes_for :images, :allow_destroy => true | |
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
# This is where an image object is created that belongs to a gallery where the :file is the paperclip object. | |
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/ | |
# def files=(files = []) | |
# files.each{|f| (@images ||= []) << images.create(file: f) } | |
# 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
class ImagesController < ApplicationController | |
before_action :set_image, only: [:show, :edit, :update, :destroy] | |
def show | |
@image = Image.find(params[:id]) | |
end | |
def index | |
@images = Image.all | |
end | |
def new | |
@image = Image.new | |
end | |
def create | |
@image = Image.new( image_params ) | |
end | |
def edit | |
end | |
private | |
# Use callbacks to share common setup or constraints between actions. | |
def set_image | |
@image = Image.find(params[:id]) | |
end | |
# Never trust parameters from the scary internet, only allow the white list through. | |
def image_params | |
params.require(:image).permit(: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
Parameters: { | |
"utf8"=>"✓", | |
"authenticity_token"=>"BJhkDx7rCRMIlXQ6T9FskXZe7+fdYxj0qm+VnCaC51w=", | |
"book"=>{ | |
"jacket_cover" =>#<ActionDispatch::Http::UploadedFile:0x000001025d3128 | |
@tempfile=#<File:/var/folders/mf/srx7jt8s2rdg0mn5hr98cvz80000gn/T/RackMultipart20140918-47766-1g9kyas>, @original_filename="559a7a477253d58f891f8e852162dfac.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"book[jacket_cover]\"; filename=\"559a7a477253d58f891f8e852162dfac.jpg\"\r\nContent-Type: image/jpeg\r\n">, | |
"title"=>"zd szw wrwr", | |
"synopsis"=>"<p>cfdcrgcgrere</p>\r\n", | |
"body"=>"<p>rccgregrrgerereg</p>\r\n", | |
"age"=>"19", | |
"publisher"=>"Dove books", | |
"author_attributes"=>{ | |
"name"=>"zsdxfrrwg", | |
"biography"=>"<p>exffwfwefewewf</p>\r\n"}, | |
"gallery_attributes"=>{ | |
"images_attributes"=>{ | |
"0"=>{ | |
"file"=>[#<ActionDispatch::Http::UploadedFile:0x000001025d20e8 @tempfile=#<File:/var/folders/mf/srx7jt8s2rdg0mn5hr98cvz80000gn/T/RackMultipart20140918-47766-tk1rdb>, @original_filename="23ebb202a3655c6d0947251cce8625b6.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"book[gallery_attributes][images_attributes][0][file][]\"; filename=\"23ebb202a3655c6d0947251cce8625b6.jpg\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0x000001025d1ff8 @tempfile=#<File:/var/folders/mf/srx7jt8s2rdg0mn5hr98cvz80000gn/T/RackMultipart20140918-47766-j3ji7c>, @original_filename="559a7a477253d58f891f8e852162dfac.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"book[gallery_attributes][images_attributes][0][file][]\"; filename=\"559a7a477253d58f891f8e852162dfac.jpg\"\r\nContent-Type: image/jpeg\r\n">] | |
} | |
} | |
} | |
}, "commit"=>"Create Book" | |
} | |
Unpermitted parameters: images_attributes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment