Created
July 19, 2016 00:59
-
-
Save jkarnowski/9ce4e17ccf4a427272d3a521ae95acb9 to your computer and use it in GitHub Desktop.
Sessions-Users-Example-CRUD
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add helpers here as *.rb files, e.g., | |
app/helpers/formatting.rb | |
helpers do | |
def em(text) | |
"<em>#{text}</em>" | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<center> | |
<html lang="en"> | |
<head> | |
<link rel="stylesheet" href="/css/normalize.css?app=skills"> | |
<link rel="stylesheet" href="/css/application.css?app=skills"> | |
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> | |
<script src="/js/application.js?app=skills"></script> | |
<title></title> | |
</head> | |
<body> | |
<% if current_user %> | |
<a href="/logout">Logout </a> | |
<% else %> | |
<a href="/users/new">Sign Up </a> | <a href="/sessions/new"> Login </a> | |
<% end %> | |
<%= yield %> | |
</body> | |
</html> | |
</center> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form action="/sessions" method="post"> | |
Enter E-mail: <input type="text" name="email"><br><br> | |
Enter Password: <input type="password" name="password"><br><br> | |
<input type="submit" value="CONFIRM"> | |
</form> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# login page | |
get '/sessions/new' do | |
erb :"/sessions/new" | |
end | |
get '/sessions-inspect' do | |
session.inspect | |
end | |
# login logic | |
# {"email"=>"[email protected]", "password"=>"123"} | |
post '/sessions' do | |
@user = User.find_by(email: params[:email]) | |
if @user && @user.password == params[:password] | |
login(@user) | |
redirect "/users/#{@user.id}" | |
# redirect "/users/#{current_user}" | |
else | |
redirect '/sessions/new' | |
end | |
# redirect "/users/#{something is going to go here}" | |
end | |
# logout logic | |
get '/logout' do | |
session[:id] = nil | |
p "You are logged out" | |
redirect '/' | |
end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
helpers do | |
def login(user) | |
session[:id] = user.id | |
end | |
# memoization | |
def current_user | |
@current_user ||= User.find(session[:id]) if session[:id] | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
has_many :subscriptions | |
has_many :channels, through: :subscriptions | |
validates :email, presence: true, uniqueness: true | |
validates :password, presence: true | |
include BCrypt | |
def password | |
@password ||= Password.new(hashed_password) | |
end | |
def password=(new_password) | |
@password = Password.create(new_password) | |
self.hashed_password = @password | |
end | |
def self.authenticate(password) | |
p self | |
p "inside MODEL" | |
if user && user.password == password | |
user | |
else | |
nil | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
get '/users/new' do | |
erb :"/users/new" | |
end | |
post '/users' do | |
@new_user = User.new(params[:user]) | |
if @new_user.save | |
login(@new_user) | |
redirect "/users/#{current_user.id}" | |
else | |
p @errors = "wrong data - try again" | |
erb :"users/new" | |
end | |
end | |
get '/users/:id' do | |
@user = User.find(params[:id]) | |
erb :'users/show' | |
end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Shows All Users</h1> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div> | |
<form action="/users" method="post"> | |
Enter First Name: <input type="text" name="user[first_name]"><br><br> | |
Enter Last Name: <input type="text" name="user[last_name]"><br><br> | |
Enter E-mail: <input type="text" name="user[email]"><br><br> | |
Enter Password: <input type="password" name="user[password]"><br><br> | |
<input type="submit" value="CONFIRM"> | |
</form> | |
</div> | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<p>Hi - to one specific user</p> | |
<%= @user.first_name %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment