Skip to content

Instantly share code, notes, and snippets.

@cacheflow
Created May 19, 2014 04:29
Show Gist options
  • Save cacheflow/52a1b2ce5209da6ba7b9 to your computer and use it in GitHub Desktop.
Save cacheflow/52a1b2ce5209da6ba7b9 to your computer and use it in GitHub Desktop.
Blogging App
class CreateAuthors < ActiveRecord::Migration
def change
create_table :authors do |t|
t.string :email
t.string :password_digest
t.timestamps
end
end
end
require "bcrypt"
class Author < ActiveRecord::Base
field :name, type: String
field :email, type: String
field :password_digest, type: String
validates :name, :email, uniqueness: :true, presence: :true
def password
@password
end
def password=(new_password)
@password = new_password
self.password_digest = BCrypt::Password.create(new_password)
end
def authenticate(login_password)
if BCrypt::Password.new(self.password_digest) == login_password
self
else
false
end
end
end
class AuthorsController < ApplicationController
def index
@authors = Author.all
respond_to do |format|
format.html
format.json {render json: @authors}
end
end
def update
@author = Author.find(params[:id])
end
def create
@author = Author.new(author_params)
if @author.save
flash[:success] = "You are now a registered user!"
redirect to :root
else
render :new
end
end
protected
def author_params
params.require(:author).permit(:name, :email, :password)
end
end
<!DOCTYPE html>
<html>
<head>
<title>Blogger</title>
<%= stylesheet_link_tag "application" %>
<%= javascript_include_tag 'application' %>
<%= csrf_meta_tags %>
</head>
<body>
<ul>
<% if !@current_user %>
<li> <%=link_to "Sign up", new_authors_path %> </li>
<li> <%=link_to "Sign in", new_sessions_path %> </li>
<% else %>
<li> Logged in as <%= current_author.name %> </li>
<li> <%= link_to "Sign Out", sessions_path, :method => :delete %>
</li>
<%end%>
</ul>
<p class="flash"><%= flash.notice %></p>
<%= yield %>
</body>
</html>
Rails.application.routes.draw do
root to: "articles#index"
resources :articles do
resources :comments
end
resources :authors
resources :tags
resource :sessions
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment