More Rails!

UD, Validations, Associations, Params

Last Week

  • Index 
  • Show
  • New
  • Create

Today:

  • Models:
    • Validations
    • Associations
  • Controller:
    • Strong parameters
  • Gems
  • Destroy
  • Edit
  • Update

AND

CR    UD

Create, read, update, destroy

  • Shows the form where you can edit your model
  • Similar to create, except the fields are filled in for you
  • Submits to the Update action

Edit

/users/5/edit (GET)

@user = User.find(params[:id)
  • Actually updates your model with the fields given
  • Usually redirects to another page.

Update

/users/5 (PUT)

user = User.find(params[:id)
user.update_attributes(user_params)

Destroy

  • Destroys the model with given id.

/users/5 (DELETE)

user = User.find(params[:id])
user.destroy
class UsersController < ApplicationController
    def edit
        @user = User.find(params[:id])
    end

    def update
        @user = User.find(params[:id])
        if @user.update_attributes(user_params)
            redirect_to user_path(@user)
        else
            render 'edit'
        end
    end

    def destroy
        @user = User.find(params[:id])
        @user.destroy
        redirect_to root_path
    end

    private

    def user_params
        params.require(:user).permit(:name)
    end
end

YOU ARE HERE

Model

Validations

Validations

validates :attribute, { validators }

OR

validates_validator_of [ :attributes ]

Associations

  • has_many 
    • A Student has many Grades
  • belongs_to 
    • Grades belong to Students

Associations

class Student < ActiveRecord::Base
   has_many :grades
end
class Grade < ActiveRecord::Base
   belongs_to :student
end
name age major id
Bob 20 EECS 1
Alice 19 Bio 2
id grade student_id
100 4 1
101 3 1

Primary Key

Foreign Key

name age major id
Bob 20 EECS 1
Alice 19 Bio 2
id grade student_id
100 4 1
101 3 1

Primary Key

Foreign Key

Associations

student = Student.find(1)
student.grades

Controller

Quick Review: Params

  • Dictionary that contains information about the request made. (Controller, action, url, etc.)
def show
    @user = User.find params[:id]
end
  • Also contains form parameters, url parameters, ids, etc.

Params

class PostsController < ApplicationController
  def index
    @post = Post.new
    @posts = Post.all
  end

  def create
    puts params
    @post = Post.new post_params
    @post.save
    redirect_to root_path
  end

  def like
  end

  private

  def post_params
    params.require(:post).permit(:text)
  end
endclass PostsController < ApplicationController
  def index
    @post = Post.new
    @posts = Post.all
  end

  def create
    puts params
    @post = Post.new post_params
    @post.save
    redirect_to root_path
  end

  def like
  end

  private

  def post_params
    params.require(:post).permit(:text)
  end
end

Params

Problem:

User

  • name
  • age
  • major
  • admin

Strong parameters

Problem:

User

  • name
  • age
  • major
  • admin

Strong parameters

BUT

curl -d "user[name]=Charles" 
     -d "user[major]=EECS" 
     -d "user[age]=20" 
     -d "user[admin]=true" 
http://localhost:3000/users

Strong Params

  • Define whitelisted attributes that you can create a model with.
def user_params
    params.require(:user).permit(:name, :age, :major)
end
curl -d "user[name]=Charles" 
     -d "user[major]=EECS" 
     -d "user[age]=20" 
     -d "user[admin]=true" 
http://localhost:3000/users

Strong Params

def create
    @user = User.new params[:user]
end
def create
    @user = User.new user_params
end

private 

def user_params
    params.require(:user).permit(:name, :age, :major)
end

BECOMES

Gems

https://rubygems.org/gems

gem 'sqlite3' => gem 'pg'

Example:

Live Demo

What did we learn today?

Project 1

https://github.com/rails-decal/proj1

Individual Project - Due Friday, October 23rd

Fall 2015 - Week 5: GrabBag Rails

By Rails Decal

Fall 2015 - Week 5: GrabBag Rails

  • 1,153