The slideshows can be found here: http://tinyurl.com/losrh8l
bin/rails new todo
cd todo/
bin/rails generate model User name:string email:string
You should have a new model file with the following code:
class User < ActiveRecord::Base
end
At this point we have created the migration file but we still have to run the migration. To run migrations type:
bin/rake db:migrate
If this is the first time you have run the migration you should get an output to the console saying a users table was created.
The rails console allows you to interact with your ruby classes within the rails environment.
In your terminal type:
bin/rails console
This will launch the console. Once you are in the console type:
User.first
That should return nil
.
To create a a new user type:
User.create(name: 'Nate', email: '[email protected]')
Now you should be able to get the first user:
User.first
# get a user
user = User.first
user.name # should print the name you set previously
user.email # should print the email you set previously
# update user
user = User.first
user.name = 'Bob'
user.save # should print true
# delete a user
user = User.first
user.destroy # should print true
# create a new user
user = User.new
user.name = 'John'
user.email = '[email protected]'
user.save
We can validate that a user must always have a name
# app/models/user.rb
class User < ActiveRecord::Base
validates :name, presence: true
end
- NOTE: if you change something in the model file you must reload your console in order for it to pick up on the changes. *
From the console run:
reload!
Now test the new validation:
user = User.create(email: '[email protected]')
The above line should return false
because we did not include a name
.
You can test to see if a record is valid or not with:
user.valid?
In our case this should return false
. Why?
Rails allows us to view any errors on a record that might be present.
user.errors
This will tell us that the name
field can not be blank.
In this app, a user
will have many tasks
. So let's create a new Task model that belongs to a user.
Back in the terminal (exit the rails console by typing exit
)
bin/rails generate model Task title user:references
This created a few files, one main one being the migration file. So, let's run our migrations.
bin/rake db:migrate
Now we have a tasks
table. We must define the relationship within the model classes.
Open up app/models/task.rb
and app/models/user.rb files and make them look like this:
# app/models/task.rb
class Task < ActiveRecord::Base
belongs_to :user
end
# app/models/user.rb
class User < ActiveRecord::Base
has_many :tasks
validates :name, presence: true
end
Test the association by opening the console again (bin/rails console
).
You should now be able to get a users tasks.
User.first.tasks
The above code should return an empty array since you have not created any tasks.
- Note: Ensure that you have at least one user by running
User.count
. If the number is zero create a new user (see above). *
In the console:
user = User.first
Task.create(title: 'Demo', user: user)
Now our first user has one task. Confirm by typing:
User.first.tasks
That should return an array with one task.
We want want a way to keep track of whether or not a task has been completed or not. Let's add a completed
column to our tasks
table.
Exit the console and type the folling into the terminal.
bin/rails generate migration AddCompletedToTasks completed:boolean
Now if you look in db/migrate
directory there will be a new file created that should look like:
class AddCompletedToTasks
add_column :tasks, :completed, :boolean
end
To test that this new column exists, boot up the rails console and type the following:
Task.first.completed
- NOTE: You should have at least one task created. *
We can update this column in the console like so:
task = Task.first
task.update_attributes(completed: true)
Generate a controller for the User model.
bin/rails generate controller Users
Start a rails server:
bin/rails server
Open your rails app in a browser at http://localhost:3000/users - You'll get an error that no route matches /users.
Open config/routes.rb
and add a route.
Rails.application.routes.draw do
get '/users' => 'users#index'
end
Open app/controllers/users_controller.rb
and add an index action.
class UsersController < ApplicationController
def index
@users = User.all
end
end
Create a file app/views/users/index.html.erb
<p>Number of users: <%= @users.count %></p>
<ul>
<% @users.each do |user| %>
<li>
<%= user.name %>
<%= user.email %>
</li>
<% end %>
</ul>
Open config/routes.rb
and add a users#show route:
get '/users/:id' => 'users#show', as: 'user'
Run bin/rake routes
to see a list of our routes.
Open app/views/users/index.html.erb
and link the user.
<p>Number of users: <%= @users.count %></p>
<ul>
<% @users.each do |user| %>
<li>
<%= user.name %>
<%= user.email %>
<%= link_to 'Show', user %>
</li>
<% end %>
</ul>
Open app/controllers/users_controller.rb
and add a show action.
def show
@user = User.find(params[:id])
end
Create a file app/views/users/show.html.erb
<h1>Showing <%= @user.name %></h1>
<p><%= @user.email %></p>
Open config/routes.rb
and add a users#show route:
get '/users/new' => 'users#new'
pen app/views/users/index.html.erb
and link the new action.
<p><%= link_to 'New User', users_new_path %></p>
Open app/controllers/users_controller.rb
and add a new action.
def new
@user = User.new
end
Create a file app/views/users/new.html.erb
<h1>New User</h1>
<%= form_for @user do |f| %>
<p>
<%= f.label :name %>
<%= f.text_field :name, placeholder: 'Your Name' %>
</p>
<p>
<%= f.label :email %>
<%= f.text_field :email, placeholder: 'Your Email' %>
</p>
<p><%= f.submit %></p>
<% end %>