Skip to content

Instantly share code, notes, and snippets.

@laurenhavertz
Last active December 19, 2015 12:59
Show Gist options
  • Select an option

  • Save laurenhavertz/5958856 to your computer and use it in GitHub Desktop.

Select an option

Save laurenhavertz/5958856 to your computer and use it in GitHub Desktop.
RAILS RESOURCES

Moma App

  1. create new app rails new moma_app
  2. Sketch out the tables
Artists Paintings
name style
DoB title
nationality date
bio artist

Create the Artist Model

rails generate model Artist

     lass CreateArtists < ActiveRecord::Migration
   def change
     create_table :artists do |t|
       t.string :name
       t.date :dob
       t.string :nationality
       t.string :period
       t.text :image
       t.timestamps
     end
   end
 end
  1. rake db:create
  2. rake db:migrate

Create the Painting Model

rails generate model Painting

class CreatePaintings < ActiveRecord::Migration
  def change
    create_table :paintings do |t|
      t.string :title
      t.string :year
      t.string :medium
      t.string :style
      t.text :image 
      t.integer :artist_id
      t.timestamps
    end
  end
end
  1. rake db:migrate
  • attr_accessible :has "read" and "write" properties like attr_accessor for rails
  1. add attributes to artist.rb and painting.rb, RELATIONSHIPS, and VALIDATIONS

    class Painting < ActiveRecord::Base attr_accessible :title, :year, :medium, :style, :image, :artist_id

     belongs_to :artist
     
     validates_presence_of :year, :artist
     validates_uniqueness_of :title
    

    end

    class Artist < ActiveRecord::Base attr_accessible :name, :nationality, :dob, :period, :image

     has_many :paintings
    

    end

  2. Add database seeds

    Artist.delete_all Painting.delete_all

    Artist.create(:name => 'Pablo Picasso', :nationality => 'Spain', :dob => '5/3/1686', :period => 'Impressionistic', :image => 'http://cdn.enjoyourholiday.com/wp-content/uploads/2012/10/Picasso-portrait.jpg')
    Artist.create(:name => 'Salvador Dali', :nationality => 'Spain', :dob => '4/9/1583', :period => 'Modern', :image => 'http://www.biography.com/imported/images/Biography/Images/Profiles/D/Salvador-Dali-40389-2-402.jpg')
    Artist.create(:name => 'Claude Monet', :nationality => 'French', :dob => '3/9/1275', :period => 'Post Modern', :image => 'http://www.biography.com/imported/images/Biography/Images/Profiles/M/Claude-Monet-WC-9411771-2-402.jpg')
    Artist.create(:name => 'Henri Matisse', :nationality => 'French', :dob => '8/3/1376', :period => 'Realists', :image => 'http://upload.wikimedia.org/wikipedia/commons/thumb/b/b1/Portrait_of_Henri_Matisse_1933_May_20.jpg/220px-Portrait_of_Henri_Matisse_1933_May_20.jpg')
     
    Painting.create(:title => 'Self Portrait', :year => '1245', :medium => 'oil', :style => 'awesome', :image => 'http://www.artcyclopedia.org/art/van-gogh-self.jpg', :artist_id => 1)
    Painting.create(:title => 'A Woman', :year => '1656', :medium => 'canvas', :style => 'cool', :image => 'http://www.arttherapyblog.com/uimages/2010/10/picasso_woman_b.jpg', :artist_id => 2)
    Painting.create(:title => 'Butterfly Ship', :year => '1837', :medium => 'water colors', :style => 'quick', :image => 'http://www.idealnetworker.com/home/idealnet/public.html/wp-content/uploads/2011/04/art-salvador-dali.jpg', :artist_id => 3)
    Painting.create(:title => 'Twilight', :year => '1488', :medium => 'fabric', :style => 'modern', :image => 'http://framingpainting.com/UploadPic/Claude%20Monet/big/Venice%20Twilight.jpg', :artist_id => 4)
    Painting.create(:title => 'Lydia', :year => '1734', :medium => 'oil', :style => 'other', :image => 'http://www.arthermitage.org/Henri-Matisse/Portrait-of-Lydia-Delectorskaya.jpg', :artist_id => 5)````
    
    
  3. rake db:seed

  4. rails console Pulling data from the Active Record First line is the SQL command

  5. Artist.all selects all information from the artist table

  6. a = artist.first SELECT "artists".* FROM "artists" in SQL

  7. a.paintings like the JOIN method in SQL SELECT "paintings".* FROM "paintings" WHERE "paintings"."artist_id" = 1 14.Artist.where(:name => "Pablo Picasso") SELECT "artists".* FROM "artists" WHERE "artists"."name" = 'Pablo Picasso'

  8. Artist.where(:name => "Pablo Picasso").first.paintings SELECT "artists".* FROM "artists" WHERE "artists"."name" = 'Pablo Picasso' LIMIT 1 SELECT "paintings".* FROM "paintings" WHERE "paintings"."artist_id" = 2

  9. Artist.find(1) SELECT "artists".* FROM "artists" WHERE "artists"."id" = ? LIMIT 1 [["id", 1]]

  10. Artist.find_by_name("Pablo Picasso") SELECT "artists".* FROM "artists" WHERE "artists"."name" = 'Pablo Picasso' LIMIT 1

  11. j= Artist.create(name: "Jackie") j.paintings.build(title: "awesome")

  12. p.errors have to assign to a variable SO you can call and rails will create the error messsage

Custom Validation

  • In order for validation to run, you must try to save to the database

    validate :ian
    
    def ian 
      if self.year.to_i < Time.now.year
        errors.add(:year, "Year is in the future")
      end
    end
    

CRUD and RESTful Routes

  • Create - NEW and CREATE

  • Read - INDEX and SHOW

  • Update - EDIT and UPDATE

  • Destroy - destroy

  • GET == RECEIVE

  • POST == CREATE

Controllers

class ArtistsController < ApplicationController


  def new #get
   @artist = Artist.new
  end

  def create #post
   @artist = Artist.create(params[:id])
   render :show
  end

  def index #get read multiple objects
    @artists = Artist.all
  end

  def show #get read one object
   @artist = Artist.find(params[:id])
  end

  def edit #get
   @artist = Artist.find(params[:id])
  end

  def update #post
   artist = Artist.find(params[:id])
   artist.update_attributes(params[:artist])
   render :show
    end

  def destroy #deletes
  artist = Artist.find(params[:id])
  artist.delete
  redirect_to(artists_path)
  end

end 

Views

  • Can use a new one called

    Edit Artists

    <%= render :partial => 'form' %>

  • connects the two html pages

  • What it looks like in the views (SAVE AS _form.html.erb)

Redirect

redirect_to is a method and the _path you can use to redirect to different route

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment