Last active
August 29, 2015 14:07
-
-
Save mdunbavan/6e350fa746f64731ed3a to your computer and use it in GitHub Desktop.
school check
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 nested-fields"> | |
| <%= f.input :school_name %> | |
| <%= f.input :address %> | |
| <%= link_to_remove_association '', f, :class => 'fa fa-minus' %> | |
| </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
| accepts_nested_attributes_for :author | |
| accepts_nested_attributes_for :gallery, :allow_destroy => true | |
| accepts_nested_attributes_for :schools, :reject_if => :find_school, :allow_destroy => true | |
| accepts_nested_attributes_for :images, :allow_destroy => true | |
| def find_school(school) | |
| if existing_school = School.find_by_school_name(school['school_name']) | |
| self.schools << existing_school | |
| return true | |
| else | |
| return false | |
| 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
| <div id="school" class="field"> | |
| <h3>Schools reading this book (add the name and full address of the school)</h3> | |
| <%= f.simple_fields_for :schools, :wrapper => 'inline' do |builder| %> | |
| <%= render 'school_fields', :f => builder %> | |
| <%= link_to_add_association 'add school', f, :schools, :render_options => {:wrapper => 'inline' }, :class => 'fa fa-plus' %> | |
| <% 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
| class BooksController < ApplicationController | |
| load_and_authorize_resource | |
| 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.text_search(params[:query]).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 | |
| @author_id = @book.author.id | |
| @similar_books = Book.where("id != '#{@current_book}'").where("author_id = '#{@author_id}'").order('created_at DESC') | |
| @gallery = @book.gallery | |
| @stories = @book.stories | |
| @author = Author.new | |
| @schools = School.where("book_id = '#{@current_book}'") | |
| end | |
| # GET /books/new | |
| def new | |
| @book = Book.new | |
| @author = Author.new | |
| @school = School.new | |
| @book.build_author | |
| @gallery = @book.build_gallery | |
| #@book.build_gallery | |
| @gallery.images.build | |
| @book.schools.build | |
| end | |
| # GET /books/1/edit | |
| def edit | |
| @book.schools.build if @book.schools.empty? | |
| 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 | |
| # PATCH/PUT /books/1 | |
| # PATCH/PUT /books/1.json | |
| 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 | |
| # DELETE /books/1 | |
| # DELETE /books/1.json | |
| 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.find(params[:id]) | |
| #@book = Book.friendly.find(params[:id]) | |
| end | |
| # Never trust parameters from the scary internet, only allow the white list through. | |
| def book_params | |
| params.require(:book).permit(:title, :synopsis, :body, :age, :publisher, :jacket_cover, :story_id, :author_id, :gallery_id, :school_id, schools_attributes: [:id, :address, :school_name, :website, :_destroy], author_attributes: [:name,:biography], :gallery_attributes => [:name, :id, :_destroy, :images_attributes => [:id, :file, :_destroy] ] ) | |
| 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment