Last active
August 29, 2015 14:06
-
-
Save mdunbavan/c58de3a492e88d4ccf91 to your computer and use it in GitHub Desktop.
Saving authors on books
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
| <%= 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, collection: 0..100, prompt: "Select age range" %> | |
| <%= f.input :publisher %> | |
| </div> | |
| <h4>Select cuurent authors if applicable: </h4> | |
| <%= f.select( :author_id, Author.all.map {|u| [u.name,u.id]}, {:include_blank => false, prompt: "No Author"} ) %> | |
| <div id="author-inputs"> | |
| <h4>If you cannot see the author above create one here!</h4> | |
| <%= 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 %> | |
| </div> | |
| <%= f.simple_fields_for :gallery do |builder| %> | |
| <%= render 'galleries/form', :f => builder %> | |
| <% end %> | |
| <%#= f.select :author_id, Author.all.collect {|a| [ a.name, a.id ] } %> | |
| <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
| 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 | |
| belongs_to :gallery | |
| has_many :stories | |
| has_many :images | |
| accepts_nested_attributes_for :author | |
| accepts_nested_attributes_for :gallery | |
| accepts_nested_attributes_for :images | |
| 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] | |
| # GET /books | |
| # GET /books.json | |
| def index | |
| @books = Book.order('created_at DESC').all | |
| end | |
| # GET /books/1 | |
| # GET /books/1.json | |
| def show | |
| # @book = Book.find(params[:id]) | |
| @book = Book.friendly.find(params[:id]) | |
| @current_book = Book.find(params[:id]).id | |
| @similar_books = Book.where("id != '#{@current_book}'").where("author_id = '#{@book.author.id}'") | |
| @gallery = @book.gallery | |
| @stories = Story.order('created_at DESC').all | |
| end | |
| # GET /books/new | |
| def new | |
| @book = Book.new | |
| @book.build_author | |
| @gallery = @book.build_gallery | |
| @gallery.images.build | |
| end | |
| # GET /books/1/edit | |
| def edit | |
| end | |
| def create | |
| #raise params.inspect | |
| @book = Book.create(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 | |
| def set_book | |
| # @book = Book.find(params[:id]) | |
| @book = Book.friendly.find(params[:id]) | |
| end | |
| def book_params | |
| params.require(:book).permit(:title, :synopsis, :body, :age, :publisher, :jacket_cover, :story_id, author_attributes: [:name,:biography], gallery_attributes: [:name, :book_id, images: []] ) | |
| 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
| ActiveRecord::Schema.define(version: 20140922113047) do | |
| # These are extensions that must be enabled in order to support this database | |
| enable_extension "plpgsql" | |
| create_table "authors", force: true do |t| | |
| t.string "name" | |
| t.text "biography" | |
| t.datetime "created_at" | |
| t.datetime "updated_at" | |
| t.integer "book_id" | |
| end | |
| create_table "books", force: true do |t| | |
| t.string "title" | |
| t.text "synopsis" | |
| t.text "body" | |
| t.datetime "created_at" | |
| t.datetime "updated_at" | |
| t.string "jacket_cover_file_name" | |
| t.string "jacket_cover_content_type" | |
| t.integer "jacket_cover_file_size" | |
| t.datetime "jacket_cover_updated_at" | |
| t.integer "author_id" | |
| t.integer "age" | |
| t.string "publisher" | |
| t.string "slug" | |
| t.integer "gallery_id" | |
| t.integer "story_id" | |
| end | |
| create_table "friendly_id_slugs", force: true do |t| | |
| t.string "slug", null: false | |
| t.integer "sluggable_id", null: false | |
| t.string "sluggable_type", limit: 50 | |
| t.string "scope" | |
| t.datetime "created_at" | |
| end | |
| add_index "friendly_id_slugs", ["slug", "sluggable_type", "scope"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type_and_scope", unique: true, using: :btree | |
| add_index "friendly_id_slugs", ["slug", "sluggable_type"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type", using: :btree | |
| add_index "friendly_id_slugs", ["sluggable_id"], name: "index_friendly_id_slugs_on_sluggable_id", using: :btree | |
| add_index "friendly_id_slugs", ["sluggable_type"], name: "index_friendly_id_slugs_on_sluggable_type", using: :btree | |
| create_table "images", force: true do |t| | |
| t.string "file" | |
| t.string "file_file_name" | |
| t.string "file_content_type" | |
| t.integer "file_file_size" | |
| t.datetime "file_updated_at" | |
| t.integer "gallery_id" | |
| end | |
| create_table "stories", force: true do |t| | |
| t.string "title" | |
| t.string "author" | |
| t.string "school" | |
| t.datetime "created_at" | |
| t.datetime "updated_at" | |
| t.string "slug" | |
| t.integer "book_id" | |
| t.text "content" | |
| t.string "image_file_name" | |
| t.string "image_content_type" | |
| t.integer "image_file_size" | |
| t.datetime "image_updated_at" | |
| 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.datetime "created_at" | |
| t.datetime "updated_at" | |
| t.string "avatar_file_name" | |
| t.string "avatar_content_type" | |
| t.integer "avatar_file_size" | |
| t.datetime "avatar_updated_at" | |
| t.string "username" | |
| end | |
| add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree | |
| add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment