How to Build a Blog with Ruby on Rails

Based on Udemy The Complete Ruby on Rails Developer Course

An error occurred while installing nokogiri (1.10.3), and Bundler
cannot continue.
Make sure that `gem install nokogiri -v '1.10.3'` succeeds before bundling.
gem update --system
xcode-select --install
#Then
gem install nokogiri
gem 'nokogiri', '~> 1.6', '>= 1.6.8'
bundle install
pages_controller.rb
class PagesController < ApplicationController def index
end
end
index.html.erb
<h1>Welcome to Blog</h1>
cd app/view
mkdir pages
cd pages
touch index.html.erb
root 'pages#index'
Rails.application.routes.draw doroot 'pages#index'

end
rails s
about.html.erb
<h1>About Blog</h1>
get 'about', to: 'pages#about'
class PagesController < ApplicationControllerdef index
end
def about
end
end
<%= link_to 'About', about_path %>
<%= link_to 'Home', root_path %>
$ rails g migration create_articles
class CreateArticles < ActiveRecord::Migration[5.1]
def change
create_table :articles do |t|
end
end
end
class CreateArticles < ActiveRecord::Migration[5.1]
def change
create_table :articles do |t|
t.string :title
t.text :description
end
end
end
$ rake db:migrate
$ rake db:rollback
$ rails g migration add_description_to_articles
class AddDescriptionToArticles < ActiveRecord::Migration
def change
end
end
class AddDescriptionToArticles < ActiveRecord::Migration
def change
add_column :articles, :description, :text
add_column :articles, :created_at, :datetime
add_column :articles, :updated_at, :datetime
end
end
$ rake db:migrate
ActiveRecord::Schema.define(version: 20180502160956) docreate_table "articles", force: :cascade do |t|
t.string "title"
t.text "description"
t.datetime "created_at"
t.datetime "updated_at"
end
end
article.rb
cd app/models
touch article.rb
class Article < ActiveRecord::Baseend
$ rails c
$ Article.all
article = Article.new
article.save
article = Article.new(title: "This is my second article", description: "This is my second description")
article.save
Article.create(title: "This is my third title", description: "This is my third description")
article = Article.find(2)
article.title = "This is an edited second article"
article.save
article = Article.find(3)
article.destroy
class Article < ActiveRecord::Base
validates :title, presence: true
end
article = Article.new
article.save
class Article < ActiveRecord::Base
validates :title, presence: true
validates :description, presence: true
end
article.errors.any?
article.errors.full_messages
class Article < ActiveRecord::Base
validates :title, presence: true, length: { minimum: 3, maximum: 50 }
validates :description, presence: true, length: { minimum: 10, maximum: 300 }
end
resources :articles
exit
$ rake routes
touch articles_controller.rb#or just create the file using the visual text editor 
class ArticlesController < ApplicationControllerend
cd views
mkdir articles
cd articles
touch new.html.erb# or create using the visual text editor
class ArticlesController < ApplicationController
def new
end
end
<h1>Create an article</h1>
<h1>Create an Article</h1><%= form_for @article do |f| %><% end %>
class ArticlesController < ApplicationControllerdef new
@article = Article.new
end
end
<h1>Create an Article</h1><%= form_for @article do |f| %>
<p>
<%= f.label :title %>
<%= f.text_field :title %>
</p>
<% end %>
<h1>Create an Article</h1><%= form_for @article do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :description %><br>
<%= f.text_area :description %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
class ArticlesController < ApplicationControllerdef new
@article = Article.new
end
def create
render plain: params[:article].inspect
end
end
class ArticlesController < ApplicationControllerdef new
@article = Article.new
end
def create
#render plain: params[:article].inspect
@article = Article.new(article_params)
@article.save
end
private
def article_params
params.require(:article).permit(:title, :description)
end
end
redirect_to articles_show(@article)
class ArticlesController < ApplicationControllerdef new
@article = Article.new
end
def create
#render plain: params[:article].inspect
@article = Article.new(article_params)
@article.save
redirect_to articles_show(@article)
end
private
def article_params
params.require(:article).permit(:title, :description)
end
end
def create
@article = Article.new(article_params)
if @article.save
flash[:notice] = "Article was successfully created"
redirect_to article_path(@article)
else
render 'new'
end
end
<% flash.each do |name, msg| %>
<ul>
<li><%= msg %></li>
</ul>
<% end %>
<% if @article.errors.any? %>
<h2>The following errors prevented the article from saving:</h2>
<ul>
<% @article.errors.full_messages.each do |msg|%>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
def show
@article = Article.find(params[:id])
end
show.html.erb
<h1>Showing selected article</h1><p>
Title: <%= @article.title %>
</p>
<p>
Description: <%= @article.description %>
</p>
def edit
end
edit.html.erb
<h1>Edit Article</h1><% if @article.errors.any? %>
<h2>The following errors prevented the article from saving:</h2>
<ul>
<% @article.errors.full_messages.each do |msg|%>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
<%= form_for @article do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :description %><br>
<%= f.text_area :description %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
def edit
@article = Article.find(params[:id])
end
def update
@article = Article.find(params[:id])
if @article.update(article_params)
flash[:notice] = "Article was updated"
redirect_to article_path(@article)
else
flash[:notice] = "Article was not updated"
render 'edit'
end
end
def index
@articles = Article.all
end
index.html.erb
<h1>Listing all articles</h1><table>
<tr>
<th>Title</th>
<th>Description</th>
</tr>
<% @articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.description %></td>
</tr>
<% end %>
</table>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Show', article_path(article) %></td>
<h1>Listing all articles</h1>
<p>
<%= link_to "Create new article", new_article_path %>
</p>
<table>
<tr>
<th>Title</th>
<th>Description</th>
</tr>
<% @articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.description %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Show', article_path(article) %></td>
</tr>
<% end %>
</table>
<%= link_to 'Back', articles_path %>
<%= link_to "Edit", edit_article_path(@article) %>
_form.html.erb
<% if @article.errors.any? %>
<h2>The following errors prevented the article from saving:</h2>
<ul>
<% @article.errors.full_messages.each do |msg|%>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
<%= form_for @article do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :description %><br>
<%= f.text_area :description %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<%= render 'form' %>
<h1>Edit Article</h1><%= render 'form' %><%= link_to 'Back', articles_path %>
def destroy
@article = Article.find(params[:id])
@article.destroy
flash[:notice] = "Article was deleted"
redirect_to articles_path
end
<%= link_to 'Delete', article_path(article), method: :delete, data: {confirm: "Are you sure?"} %>
<h1>Listing all articles</h1>
<p>
<%= link_to "Create new article", new_article_path %>
</p>
<table>
<tr>
<th>Title</th>
<th>Description</th>
</tr>
<% @articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.description %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Show', article_path(article) %></td>
<td><%= link_to 'Delete', article_path(article), method: :delete, data: {confirm: "Are you sure?"} %></td>
</tr>
<% end %>
</table>
<%= link_to 'Delete', article_path(@article), method: :delete, data: {confirm: "Are you sure?"} %>
<%= link_to 'Delete', article_path(article), method: :delete, data: {confirm: "Are you sure?"} %>
@article = Article.find(params[:id])
def set_article
@article = Article.find(params[:id])
end
before_action :set_article, only: [:edit, :update, :show, :destroy]
class ArticlesController < ApplicationController
before_action :set_article, only: [:edit, :update, :show, :destroy]
def new
@article = Article.new
end
def create
@article = Article.new(article_params)
if @article.save
flash[:notice] = "Article was successfully created"
redirect_to article_path(@article)
else
render 'new'
end
end
def show
end
def update
if @article.update(article_params)
flash[:notice] = "Article was updated"
redirect_to article_path(@article)
else
flash[:notice] = "Article was not updated"
render 'edit'
end
end
def edit
end
def index
@articles = Article.all
end
def destroy
@article.destroy
flash[:notice] = "Article was deleted"
redirect_to articles_path
end
private
def article_params
params.require(:article).permit(:title, :description)
end
def set_article
@article = Article.find(params[:id])
end
end
gem 'bootstrap-sass', '~> 3.4.1'
gem 'sassc-rails', '>= 2.1.0'
$ bundle install
custom.css.scss
// "bootstrap-sprockets" must be imported before "bootstrap" and "bootstrap/variables"
@import "bootstrap-sprockets";
@import "bootstrap";
//= require jquery
//= require bootstrap-sprockets
gem 'jquery-rails'
$ rails -v
$ bundle install
_navigation.html.erb
<%= render 'layouts/navigation' %>
<!DOCTYPE html>
<html>
<head>
<title>Blog</title>
<%= csrf_meta_tags %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>
<body>
<%= render 'layouts/navigation' %>
<% flash.each do |name, msg| %>
<ul>
<li><%= msg %></li>
</ul>
<% end %>
<%= yield %>
</body>
</html>
$ rails s
$navbar-default-bg: black;@import "bootstrap-sprockets";
@import "bootstrap";
#logo {
float: left;
font-size: 1.7em;
color: white;
text-transform: uppercase;
letter-spacing: -1px;
font-weight: bold;
}
#logo:hover {
color: #fff;
text-decoration: none;
}
.article-actions {
border-top: 1px solid #eaeaea;
padding-top: 5px;
}
.article-title {
font-weight: bold;
font-size: 1.5em;
}
.article-body {
border-top: 1px solid #eaeaea;
padding-top: 15px;
padding-bottom: 15px;
}
.article-meta-details {
border-top: 1px solid #eaeaea;
margin-top: 15px;
}
.description {
margin-top: 0;
}
.listing {
list-style: none;
padding-left: 0;
}
<a class="navbar-brand" href="#" id="logo">Alpha Blog</a>
<%= link_to "Alpha Blog", root_path, class: "navbar-brand", id: "logo" %>
<li class="active"><a href="#">Link <span class="sr-only">(current)</span></a></li>
<li><%= link_to "Articles", articles_path %></li>
<li><a href="#">Link</a></li>
<li><%= link_to "Create Article", new_article_path %></li>
<form class="navbar-form navbar-left">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">Separated link</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">One more separated link</a></li>
<% if @article.errors.any? %>
<h2>The following errors prevented the article from saving:</h2>
<ul>
<% @article.errors.full_messages.each do |msg|%>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
<%= form_for @article do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :description %><br>
<%= f.text_area :description %>
</p>
<p>
<%= f.submit %>
</p>
<% end %><%= link_to 'Back', articles_path %>
<% if @article.errors.any? %>
<h2>The following errors prevented the article from saving:</h2>
<ul>
<% @article.errors.full_messages.each do |msg|%>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
<div class="row">
<div class="col-xs-12">
<%= form_for(@article, :html => {class: "form-horizontal", role: "form"}) do |f| %>
<div class="form-group">
<div class="control-label col-sm-2">
<%= f.label :title %>
</div>
<div class="col-sm-6">
<%= f.text_field :title, class: "form-control", placeholder: "Title of article", autofocus: true %>
</div>
</div>
<div class="form-group">
<div class="control-label col-sm-2">
<%= f.label :description %>
</div>
<div class="col-sm-6">
<%= f.text_area :description, rows: 10, class: "form-control", placeholder: "Body of article" %>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<%= f.submit class: 'btn btn-primary btn-large' %>
</div>
</div>
<% end %>
</div>
</div>
<%= form_for(@article, :html => {class: "form-horizontal", role: "form"}) do |f| %>
<%= f.text_field :title, class: "form-control", placeholder: "Title of article", autofocus: true %>
<% if @article.errors.any? %>
<h2>The following errors prevented the article from saving:</h2>
<ul>
<% @article.errors.full_messages.each do |msg|%>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
<% if @article.errors.any? %>
<div class="row">
<div class="col-xs-8 col-xs-offset-2">
<div class="panel panel-danger">
<div class="panel-heading">
<h2 class="panel-title">
<%= pluralize(@article.errors.count, "error") %>
prohibited this article from being saved:
</h2>
<div class="panel-body">
<ul>
<% @article.errors.full_messages.each do |msg|%>
<li><%= msg %></li>
<% end %>
</ul>
</div>
</div>
</div>
</div>
</div>
<% end %>
<%= render 'shared/errors' %>
<% flash.each do |name, msg| %>
<ul>
<li><%= msg %></li>
</ul>
<% end %>
<%= render 'shared/errors' %>
<!DOCTYPE html>
<html>
<head>
<title>Blog</title>
<%= csrf_meta_tags %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>
<body>
<%= render 'layouts/navigation' %>
<%= render 'shared/messages' %>
<%= yield %>
</body>
</html>
<% flash.each do |name, msg| %>
<ul>
<li><%= msg %></li>
</ul>
<% end %>
<div class="row">
<div class="col-xs-10 col-xs-offset-1">
<% flash.each do |name, msg| %>
<div class='alert alert-<%= "#{name}" %>'>
<a href="#" class="close" data-dismiss="alert">&#215;</a>
<%= content_tag :div, msg, :id => "flash_#{name}" if msg.is_a?(String) %>
</div>
<% end %>
</div>
</div>
<% if obj.errors.any? %>
<div class="row">
<div class="col-xs-8 col-xs-offset-2">
<div class="panel panel-danger">
<div class="panel-heading">
<h2 class="panel-title">
<%= pluralize(obj.errors.count, "error") %>
prohibited this object from being saved:
</h2>
<div class="panel-body">
<ul>
<% obj.errors.full_messages.each do |msg|%>
<li><%= msg %></li>
<% end %>
</ul>
</div>
</div>
</div>
</div>
</div>
<% end %>
<%= render 'shared/errors' %>
<%= render 'shared/errors', obj: @article %>
<h2 align="center">Title: <%= @article.title %></h2>
<div class="well col-xs-8 col-xs-offset-2">
<h4 class="center"><strong>Description:</strong></h4>
<hr>
<%= @article.description %>
<%= link_to "Edit", edit_article_path(@article) %> |
<%= link_to 'Back', articles_path %>
<%= @article.description %>
<%= simple_format(@article.description) %>
<%= link_to "Edit", edit_article_path(@article), class: "btn btn-xs btn-primary" %> |
<%= link_to 'Back', articles_path, class: "btn btn-xs btn-success" %>
<%= link_to "Delete", article_path(@article), method: :delete, data: { confirm: "Are you sure you want to delete this article?"}, class: "btn btn-xs btn-danger" %>
<h2 align="center">Title: <%= @article.title %></h2>
<div class="well col-xs-8 col-xs-offset-2">
<h4 class="center"><strong>Description:</strong></h4>
<hr>
<%= simple_format(@article.description) %>
<%= link_to "Edit", edit_article_path(@article), class: "btn btn-xs btn-primary" %> |
<%= link_to 'Back', articles_path, class: "btn btn-xs btn-success" %>
<%= link_to "Delete", article_path(@article), method: :delete, data: { confirm: "Are you sure you want to delete this article?"}, class: "btn btn-xs btn-danger" %>
<h1 align="center">Listing all articles</h1><% @articles.each do |article| %>
<div class="row">
<div class="col-xs-8 col-xs-offset-2">
<div class="well well-lg">
<div class="article-title">
<%= link_to article.title, article_path(article) %>
</div>
<div class="article-body">
<%= truncate(article.description, length: 100) %>
</div>
<div class="article-actions">
<%= link_to "Edit", edit_article_path(article), class: "btn btn-xs btn-primary" %>
<%= link_to "Delete", article_path(article), method: :delete, data: { confirm: "Are you sure you want to delete this article?"}, class: "btn btn-xs btn-danger" %>
</div>
</div>
</div>
</div>
<% end %>
.article-body {
border-top: 1px solid #eaeaea;
padding-top: 15px;
padding-bottom: 15px;
}
.article-title {
font-weight: bold;
font-size: 1.5em;
}
$ git checkout -b create-users
$ git branch
$ git checkout master
$ git checkout create-users
$ rails g migration create_users
class CreateUsers < ActiveRecord::Migration[5.1]
def change
create_table :users do |t|
end
end
end
class CreateUsers < ActiveRecord::Migration[5.1]
def change
create_table :users do |t|
t.string :username
t.string :email
t.timestamps
end
end
end
$ rake db:migrate
class User < ActiveRecord::Baseend
$ rails c
> User.all
> user = User.create(username: "david", email: "david@example.com")
> User.all
> user = User.find(1)
> user.email = "david1@example.com"
> user.save
> user = User.find(1)
> user.destroy
> User.all
> exit
$ git status
$ git add .
$ git commit -m "create users table and model"
$ git checkout master
$ git merge create-users
$ git branch -d create-users
$ git push
$ git checkout -b user-validations
validates :username, presence: true, uniqueness: true, length: { minimum: 3, maximum: 25 }
validates :email, presence: true, uniqueness: true
class User < ActiveRecord::Base
validates :username, presence: true, uniqueness: true, length: { minimum: 3, maximum: 25 }
validates :email, presence: true, uniqueness: true

end
$ rails c
validates :username, presence: true, uniqueness: { case_sensitive: false }, length: { minimum: 3, maximum: 25 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 105 }, uniqueness: { case_sensitive: false }, 
format: { with: VALID_EMAIL_REGEX }
class User < ActiveRecord::Base
validates :username, presence: true, uniqueness: { case_sensitive: false }, length: { minimum: 3, maximum: 25 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 105 }, uniqueness: { case_sensitive: false },
format: { with: VALID_EMAIL_REGEX }
before_save { self.email = email.downcase }
end
$ git add .
$ git commit -m "added user validations"
$ git checkout master
$ git merge user-validations
$ git push
$ git branch -d user-validations
$ git checkout -b userarticle-association
$ rails g migration add_user_id_to_articles
def change
add_column :articles, :user_id, :integer
end
class AddUserIdToArticles < ActiveRecord::Migration[5.1]
def change
add_column :articles, :user_id, :integer
end
end
$ rake db:migrate
$ rails c
> Article.all
has_many :articles
belongs_to :user
validates :user_id, presence: true
before_save { self.email = email.downcase }
$ rails c
> article = Article.new(title: "this is a title", description: "this is a description)
> article.save
> article.error.full_messages
@article.user = User.first
def create
@article = Article.new(article_params)
@article.user = User.first
if @article.save
flash[:notice] = "Article was successfully created"
redirect_to article_path(@article)
else
render 'new'
end
end
class User < ActiveRecord::Base
has_many :articles
validates :username, presence: true, uniqueness: { case_sensitive: false }, length: { minimum: 3, maximum: 25 }
validates :email, presence: true, length: { maximum: 105 }, uniqueness: { case_sensitive: false },
format: { with: VALID_EMAIL_REGEX }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
before_save { self.email = email.downcase }
end
class Article < ActiveRecord::Base
belongs_to :user
validates :title, presence: true, length: { minimum: 3, maximum: 50 }
validates :description, presence: true, length: { minimum: 10, maximum: 300 }
validates :user_id, presence: true
end
<%= debug(params) if Rails.env.development? %>
$ git add .
$ git commit -m "add association between user and articles"
$ git checkout master
$ git merge userarticles-association
$ git push
$ git branch -d userarticles-association
<small>Created by: <%= article.user.username %>,
<%= time_ago_in_words(article.created_at) %> ago,
last updated: <%= time_ago_in_words(article.updated_at) %> ago</small>
<small>Created by: <%= article.user.username if article.user %>,
<%= time_ago_in_words(article.created_at) %> ago,
last updated: <%= time_ago_in_words(article.updated_at) %> ago</small>
$ rails g migration add_timestamp_to_articles
class AddTimestampToArticles < ActiveRecord::Migration[5.1]
def change
# add new column but allow null values
add_timestamps :articles, null: true
# backfill existing record with created_at and updated_at
# values making clear that the records are faked
long_ago = DateTime.new(2000, 1, 1)
Article.update_all(created_at: long_ago, updated_at: long_ago)
# change not null constraints
change_column_null :articles, :created_at, false
change_column_null :articles, :updated_at, false
end
end
rake db:migrate
<div class="article-meta-details">//stuff//</div>
.article-meta-details {
border-top: 1px solid #eaeaea;
margin-top: 15px;
}
<h1 align="center">Listing all articles</h1>
<% @articles.each do |article| %>
<div class="row">
<div class="col-xs-8 col-xs-offset-2">
<div class="well well-lg">
<div class="article-title">
<%= link_to article.title, article_path(article) %>
</div>
<div class="article-body">
<%= truncate(article.description, length: 100) %>
</div>
<div class="article-meta-details">
<small>Created by: <%= article.user.username if article.user %>, <%= time_ago_in_words(article.created_at) %> ago, last updated: <%= time_ago_in_words(article.updated_at) %> ago</small>
</div>
<div class="article-actions">
<%= link_to "Edit", edit_article_path(article), class: "btn btn-xs btn-primary" %>
<%= link_to "Delete", article_path(article), method: :delete, data: { confirm: "Are you sure you want to delete this article?"}, class: "btn btn-xs btn-danger" %>
</div>
</div>
</div>
</div>
<% end %>
has_secure_password
gem 'bcrypt', '~> 3.1.7'
$ bundle install
$ rails g migration add_password_digest_to_users
class AddPasswordDigestToUsers < ActiveRecord::Migration[5.1]
def change
add_column :users, :password_digest, :string
end
end
add_column, :users, :password_digest, :string
rake db:migrate
$ rails c
> user = User.last
> user.password = "password"
> user.save
get 'signup', to: 'users#new'
class UsersController < ApplicationControllerdef new
end
end
<%= render 'shared/errors', obj: @user %><div class="row">
<div class="col-xs-12">
<%= form_for(@user, :html => {class: "form-horizontal", role: "form"}) do |f| %>
<div class="form-group">
<div class="control-label col-sm-2">
<%= f.label :username %>
</div>
<div class="col-sm-8">
<%= f.text_field :username, class: "form-control", placeholder: "Enter username", autofocus: true %>
</div>
</div>
<div class="form-group">
<div class="control-label col-sm-2">
<%= f.label :email %>
</div>
<div class="col-sm-8">
<%= f.email_field :email, class: "form-control", placeholder: "Enter email" %>
</div>
</div>
<div class="form-group">
<div class="control-label col-sm-2">
<%= f.label :password %>
</div>
<div class="col-sm-8">
<%= f.password_field :password, class: "form-control", placeholder: "Enter password" %>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<%= f.submit(@user.new_record? ? "Sign up" : "Update account", class: 'btn btn-primary btn-large') %>
</div>
</div>
<% end %>
<div class="col-xs-4 col-xs-offset-2">
[ <%= link_to 'Cancel request and Return to Articles Listing', articles_path %> ]
</div>
</div>
</div>
<%= render 'shared/errors', obj: @user %>
<%= render 'shared/errors', obj: @articles %>
<h1 align="center">Signup</h1><%= render 'form' %>
def new
@user = User.new
end
resources :users, except: [:new]
def create
end
def create
@user = User.new(user_params)
end
privatedef user_params
params.require(:user).permit(:username, :email, :password)
end
def create
@user = User.new(user_params)
if @user.save
flash[:success] = "Welcome to the alpha blog #{@user.username}"
redirect_to articles_path
else
render 'new'
end
end
def edit
@user = User.find(params[:id])
end
<h1 align="center">Edit User Account</h1><%= render 'form' %>
def update
@user = User.find(params[:id])
if @user.update(user_params)
flash[:success] = "Your account was updated successfully"
redirect_to articles_path
else
render 'edit'
end
end
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<%= f.submit(@user.new_record? ? "Sign up" : "Update account", class: 'btn btn-primary btn-large') %>
</div>
</div>
def show
@user = User.find(params[:id])
end
<h1 align="center">Welcome to <%= @user.username %>'s page</h1>
<h1 align="center">Welcome to <%= @user.username %>'s page</h1>
<div class="row">
<div class="col-md-4 col-md-offset-4 center">
<%= gravatar_for @user %>
</div>
</div>
<h4 align="center"><%= @user.username %>'s articles</h4>
module ApplicationHelper
def gravatar_for(user)
end
end
module ApplicationHelper
def gravatar_for(user)
gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}"
image_tag(gravatar_url, alt: user.username, class: "img-circle")
end
end
module ApplicationHelper
def gravatar_for(user, options = { size: 80})
gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
size = options[:size]
gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}"
image_tag(gravatar_url, alt: user.username, class: "img-circle")
end
end
<%= gravatar_for @user, size: 150 %>
<%= gravatar_for @user %>
<% obj.each do |article| %>
<div class="row">
<div class="col-xs-8 col-xs-offset-2">
<div class="well well-lg">
<div class="article-title">
<%= link_to article.title, article_path(article) %>
</div>
<div class="article-body">
<%= truncate(article.description, length: 100) %>
<div class="article-meta-details">
<small>Created by: <%= article.user.username if article.user %>,
<%= time_ago_in_words(article.created_at) %> ago,
last updated: <%= time_ago_in_words(article.updated_at) %> ago</small>
</div>
</div>
<div class="article-actions">
<%= link_to "Edit", edit_article_path(article), class: "btn btn-xs btn-primary" %>
<%= link_to "Delete", article_path(article), method: :delete, data: { confirm: "Are you sure you want to delete this article?"}, class: "btn btn-xs btn-danger" %>
</div>
</div>
</div>
</div>
<% end %>
<% obj.each do |article| %>
<% @articles.each do |article| %>
<%= render 'shared/article', obj: @articles %>
<%= render 'article' %>
<%= render 'shared/article', obj: @user.articles %>
<h1 align="center">Welcome to <%= @user.username %>'s page</h1>
<div class="row">
<div class="col-md-4 col-md-offset-5 center">
<%= gravatar_for @user, size: 150 %>
</div>
</div>
<h4 align="center"><%= @user.username %>'s articles</h4><%= render 'shared/article', obj: @user.articles %>
def index
@users = User.all
end
<h1 align="center">All Users</h1><% @users.each do |user| %><% end %>
<h1 align="center">All Users</h1><% @users.each do |user| %>
<ul class="listing">
<div class="row">
<div class="well col-md-4 col-md-offset-4">
<li><%= link_to gravatar_for(user), user_path(user) %></li>
<li class="article-title"><%= link_to user.username, user_path(user) %></li>
<li><small><%= pluralize(user.articles.count, "article") if user.articles %></small></li>
</div>
</div>
</ul>
<% end %>
.listing {
list-style: none;
padding-left: 0;
}
<div align="center">// all the stuff //</div>
<h1 align="center">All Users</h1>
<div align="center">

<% @users.each do |user| %>
<ul class="listing">
<div class="row">
<div class="well col-md-4 col-md-offset-4">
<li><%= link_to gravatar_for(user), user_path(user) %></li>
<li class="article-title"><%= link_to user.username, user_path(user) %></li>
<li><small><%= pluralize(user.articles.count, "article") if user.articles %></small></li>
</div>
</div>
</ul>
<% end %>
</div>
<ul class="listing">
<div class="row" align="center">
<div class="col-md-4 col-md-offset-4">
<li><%= link_to gravatar_for(@article.user), user_path(@article.user) %></li>
<li class="article-title"><%= link_to @article.user.username, user_path(@article.user) %></li>
<li><small><%= pluralize(@article.user.articles.count, "article") if @article.user.articles %></small></li>
</div>
</div>
</ul>
<% if @article.user %>show the user profile...<% end %>
<h2 align="center">Title: <%= @article.title %></h2>
<div class="well col-xs-8 col-xs-offset-2">
<% if @article.user %>
<ul class="listing">
<div class="row" align="center">
<div class="col-md-4 col-md-offset-4">
<li><%= link_to gravatar_for(@article.user), user_path(@article.user) %></li>
<li class="article-title"><%= link_to @article.user.username, user_path(@article.user) %></li>
<li><small><%= pluralize(@article.user.articles.count, "article") if @article.user.articles %></small></li>
</div>
</div>
</ul>
<% end %>
<h4 class="center description"><strong>Description:</strong></h4>
<hr>
<%= simple_format(@article.description) %>
<div class="article-actions">
<%= link_to "Edit", edit_article_path(@article), class: "btn btn-xs btn-primary" %>
<%= link_to "Delete", article_path(@article), method: :delete, data: { confirm: "Are you sure you want to delete this article?"}, class: "btn btn-xs btn-danger" %>
<%= link_to "Back", articles_path, class: "btn btn-xs btn-success" %>
</div>
</div>
gem 'will_paginate', '3.1.5'
gem 'bootstrap-will_paginate', '1.0.0'
$ bundle install
@articles = Article.paginate(page: params[:page], per_page: 5)
def index
@articles = Article.paginate(page: params[:page], per_page: 5)
end
<div align="center">
<%= will_paginate %>
</div>
<h1 align="center">Listing all articles</h1><%= render 'shared/article', obj: @articles %><div align="center">
<%= will_paginate %>
</div>
def index
@users = User.paginate(page: params[:page], per_page: 5)
end
<%= will_paginate %>
<h1 align="center">All Users</h1>
<div align="center">

<% @users.each do |user| %>
<ul class="listing">
<div class="row">
<div class="well col-md-4 col-md-offset-4">
<li><%= link_to gravatar_for(user), user_path(user) %></li>
<li class="article-title"><%= link_to user.username, user_path(user) %></li>
<li><small><%= pluralize(user.articles.count, "article") if user.articles %></small></li>
</div>
</div>
</ul>
<% end %>
<%= will_paginate %>
</div>
def show
@user = User.find(params[:id])
@user_articles = @user.articles.paginate(page: params[:page], per_page: 5)
end
<div align="center">
<%= will_paginate @user_articles %>
</div>
<h1 align="center">Welcome to <%= @user.username %>'s page</h1>
<div class="row">
<div class="col-md-4 col-md-offset-5 center">
<%= gravatar_for @user, size: 150 %>
</div>
</div>
<h4 align="center"><%= @user.username %>'s articles</h4><%= render 'shared/article', obj: @user.articles %><div align="center">
<%= will_paginate @user_articles %>
</div>
get 'login', to: 'sessions#new'
class SessionsController < ApplicationControllerdef new
end
def create
end
def destroy
end
end
post 'login', to: 'sessions#create'
delete 'logout', to: 'sessions#destroy'
Rails.application.routes.draw do
resources :articles
root 'pages#index'
get 'about', to: 'pages#about'
get 'signup', to: 'users#new'
resources :users, except: [:new]
get 'login', to: 'sessions#new'
post 'login', to: 'sessions#create'
delete 'logout', to: 'sessions#destroy'
end
<%= form_for(:session, :html => {class: "form-horizontal", role: "form"}, url: login_path) do |f| %>
<div class="form-group">
<div class="control-label col-sm-2">
<%= f.label :email %>
</div>
<div class="col-sm-8">
<%= f.text_field :email, class: "form-control", placeholder: "Enter email", autofocus: true %>
</div>
</div>
<div class="form-group">
<div class="control-label col-sm-2">
<%= f.label :password %>
</div>
<div class="col-sm-8">
<%= f.password_field :password, class: "form-control", autocomplete: "off" %>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<%= f.submit "Log in", class: 'btn btn-primary btn-large' %>
</div>
</div>
<% end %>
def create 
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
session[:user_id] = user.id
flash[:success] = "You have logged in"
redirect_to users_path(user)
else
flash.now[:danger] = "There was something wrong with your login information"
render 'new'
end
end
def destroy
session[:user_id] = nil
flash[:success] = "You have logged out"
redirect_to root_path
end
<li><%= link_to "Log out", logout_path, method: :delete %></li>
def current_userenddef logged_in?enddef require_userend
helper_method :current_user, :logged_in?
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
def logged_in?
!!current_user
end
def require_user
if !logged_in?
flash[:danger] = "You must be logged in to perform that action"
redirect_to root_path
end
end
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
helper_method :current_user, :logged_in?
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
def logged_in?
!!current_user
end
def require_user
if !logged_in?
flash[:danger] = "You must be logged in to perform that action"
redirect_to root_path
end
end
end
<% if logged_in? %><% else %><% end %>

Documentation and tutorials on Python, Programming, and Data Analysis.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store