Created
September 22, 2014 14:18
-
-
Save mdunbavan/bd640c1a87baed0fdb1e to your computer and use it in GitHub Desktop.
authors save post
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
| # 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 | |
| # POST /books | |
| # POST /books.json | |
| 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 |
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 form | |
| <%= 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 %> | |
| <%= f.select( :author_id, Author.all.map {|u| [u.name,u.id]}, {:include_blank => false}) %> | |
| <%#= f.select :author_id, Author.all.collect {|a| [ a.name, a.id ] } %> | |
| <div class="actions"> | |
| <%= f.button :submit %> | |
| </div> | |
| <% end %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment