Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
"Nitrous.IO: Rails Development in the Cloud" article for RubySource.com (http://www.sitepoint.com/nitrous-io-rails-development-cloud/) Supporting code
<!-- app/views/layouts/application.html.erb -->
<!DOCTYPE html>
<head>
<title>Your Own User Zero Effort Reset</title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<ul id="nav">
<% unless user_signed_in? %>
<li><%= link_to "Log In", sign_in_path %></li>
<li><%= link_to "Register", sign_up_path %></li>
<% end %>
<% if user_signed_in? %>
<li><%= link_to "Log Out", log_out_path %></li>
<% end %>
</ul>
<%- flash.each do |key, msg| -%>
<div id="<%= key %>">
<p style="float:right;"><a href="#">X</a></p>
<p><%= msg %></p>
<div class="clear"></div>
</div>
<%- end -%>
<%= yield %>
<div id="wrap"></div>
<div id="foot">Application developed by Thom Parkin / Websembly, LLC</div>
</body>
</html>
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery
private
def authenticate_user!
if current_user.nil?
redirect_to login_url, :alert => "You must first log in to access this page"
end
end
def current_user
begin
@current_user ||= session[:user_id] && User.find(session[:user_id])
rescue
end
end
def user_signed_in?
begin
User.where("id=?", session[:user_id]).exists?
rescue
false
end
end
helper_method :current_user, :user_signed_in?, :authenticate_user?
end
Supporting code for "Nitrous.IO: Rails Development in the Cloud" which demonstrates Nitrous.io for Ruby on Rails development without regard to hardware or physical location.
Full article at http://www.sitepoint.com/nitrous-io-rails-development-cloud/
#Gemfile
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.0'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'
group :doc do
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', require: false
end
# Use ActiveModel has_secure_password
gem 'bcrypt-ruby', '~> 3.0.0'
gem 'protected_attributes'
# Use unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
gem 'capistrano', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]
<!-- app/views/sessions/index.html.erb -->
<h1>Landing Page</h1>
<h3>THIS SPACE FOR RENT</h3>
<p>Visit <a href="http://www.rubysource.com">RubySource</a> for great articles and tips on Ruby Development.</p>
# app/config/routes.rb
Youzer::Application.routes.draw do
get "users/new"
get "users/create"
get "sessions/index"
get "sessions/new"
get "sessions/create"
get "sessions/logout"
get "sessions/destroy"
match 'sign_in' => 'sessions#new', :via => :get
match 'sign_up' => 'users#new', :via => :get
match 'log_out' => 'sessions#destroy', :via => :get
resources :sessions
resources :users
root :to => 'sessions#index'
end
<!-- app/views/sessions/new.html.erb -->
<%= form_tag sessions_path do %>
<h3>Log in</h3>
<div class="field">
<%= label_tag :email %>
<%= text_field_tag :email, params[:email] %>
</div>
<div class="field">
<%= label_tag :password %>
<%= password_field_tag :password %>
</div>
<div class="actions"><%= submit_tag "Log in" %></div>
<% end %>
# app/controllers/sessions_controller.rb
class SessionsController < ApplicationController
def index
end
def new
end
def create
user = User.find_by_email(params[:email].downcase)
if user && user.authenticate(params[:password])
session[:user_id] = user.id
redirect_to root_url, :notice => "Welcome #{user.name}" #you may wish to modify this redirect
else
flash.now.alert = "Invalid email or password"
render "new"
end
end
def logout
self.destroy
end
def destroy
session[:user_id] = nil
redirect_to root_url, :notice => "Goodbye"
end
end
rails new youzer
cd youzer
rails g model User name:string email:string password:string password_confirmation:string password_digest:string
rails g controller Users new create
rails g controller Sessions index new create logout destroy
============================================================
RAILS_ENV=development && bundle exec rake db:create
RAILS_ENV=development && bundle exec rake db:migrate
# app/models/user.rb
class User < ActiveRecord::Base
attr_accessible :email, :name, :password, :password_confirmation, :password_digest
has_secure_password #This is the Rails 3 'magic'
validates_presence_of :password, :on => :create
end
<!-- app/views/users/new.html.erb -->
<%= form_for @user do |f| %>
<% if @user.errors.any? %>
<div class="error_messages">
<h2>Form is invalid</h2>
<ul>
<% for message in @user.errors.full_messages %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<h3>Sign Up</h3>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %>
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
</div>
<div class="actions"><%= f.submit "Register" %></div>
<% end %>
# app/controllers/users_controller.rb
class UsersController < ApplicationController
def new
@user = User.new
end
def create
@user = User.new(params[:user])
@user.name = params[:user][:name]
@user.email = (params[:user][:email]).downcase
if @user.save
session[:user_id] = @user.id
current_user #call in application helper
redirect_to :root, :notice => "Registered!" #you may wish to modify this redirect
else
render "new"
end
rescue Exception => ex
logger.warn('ERROR: ' + ex.message)
flash.now[:error] = 'There was an error creating the user.'
render :action => 'new'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment