Skip to content

Instantly share code, notes, and snippets.

@maasha
Last active December 16, 2015 02:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maasha/5362974 to your computer and use it in GitHub Desktop.
Save maasha/5362974 to your computer and use it in GitHub Desktop.
class User < ActiveRecord::Base
attr_accessible :email, :name, :password, :password_confirmation
has_secure_password
has_many :scaffolds, dependent: :destroy
before_save { |user| user.email = email.downcase }
before_save :create_remember_token
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
PER_PAGE = 30 # for pagination
validates :name, presence: true, length: { maximum: 50 }
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
validates :password, length: { minimum: 6 }
validates :password_confirmation, presence: true
private
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
end
class UsersController < ApplicationController
before_filter :signed_in_user, only: [:edit, :update, :index, :destroy]
before_filter :correct_user, only: [:edit, :update]
before_filter :admin_user, only: :destroy
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
sign_in @user
flash[:success] = "Welcome to Sporenstregs!"
redirect_to @user
else
render 'new'
end
end
def show
@user = User.find(params[:id])
@scaffolds = @user.scaffolds.paginate(page: params[:page], per_page: Scaffold::PER_PAGE)
end
def edit
end
def update
if @user.update_attributes(params[:user])
flash[:success] = "Profile updated"
sign_in @user
redirect_to @user
else
render 'edit'
end
end
def index
@users = User.paginate(page: params[:page], per_page: User::PER_PAGE)
end
def destroy
User.find(params[:id]).destroy
flash[:success] = "User destroyed."
redirect_to root_url
end
private
def correct_user
@user = User.find(params[:id])
redirect_to(root_path) unless current_user?(@user)
end
def admin_user
redirect_to(root_path) unless current_user.admin?
end
end
<% provide(:title, "Edit user") %>
<h1>Update your profile</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirm Password" %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Save changes", class: "btn btn-large btn-primary" %>
<% end %>
<%= gravatar_for @user %>
<p><a href="http://gravatar.com/emails">Change Gravatar</a><p>
<% unless current_user.admin? %>
<!-- <p><%= link_to "Delete Profile", @user, method: :delete, data: { confirm: "Confirm delete" } %></p> -->
<p><%= button_to "Delete Profile", { :action => "destroy", :id => @user.id }, :confirm => "Confirm delete", :method => :delete %></p>
<p><%= button_to "Delete", user_path(@user), method: :delete, confirm: "Are you sure?" %></p>
<% end %>
</div>
</div>
Sporenstregs::Application.routes.draw do
resources :users
resources :scaffolds
resources :sessions, only: [:new, :create, :destroy]
root :to => 'static_pages#home'
match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete
match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
match '/contact', to: 'static_pages#contact'
match '/news', to: 'static_pages#news'
# The priority is based upon order of creation:
# first created -> highest priority.
# Sample of regular route:
# match 'products/:id' => 'catalog#view'
# Keep in mind you can assign values other than :controller and :action
# Sample of named route:
# match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
# This route can be invoked with purchase_url(:id => product.id)
# Sample resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Sample resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Sample resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Sample resource route with more complex sub-resources
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', :on => :collection
# end
# end
# Sample resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
# root :to => 'welcome#index'
# See how all your routes lay out with "rake routes"
# This is a legacy wild controller route that's not recommended for RESTful applications.
# Note: This route will make all actions in every controller accessible via GET requests.
# match ':controller(/:action(/:id))(.:format)'
end
__END__
Log output
FROM HERE
Started DELETE "/users/101" for 127.0.0.1 at 2013-04-11 14:31:12 +0200
Processing by UsersController#destroy as HTML
Parameters: {"authenticity_token"=>"o0teiGOEPJ7YCkrBpEQpSQiACn6KLbQZ2SsHEWb3nr4=", "id"=>"101"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'UiqM3lDjBQgoJqSc8P_apQ' LIMIT 1
Redirected to http://localhost:3000/
Filter chain halted as :admin_user rendered or redirected
Completed 302 Found in 6ms (ActiveRecord: 0.2ms)
Started GET "/" for 127.0.0.1 at 2013-04-11 14:31:12 +0200
Processing by StaticPagesController#home as HTML
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'UiqM3lDjBQgoJqSc8P_apQ' LIMIT 1
Redirected to http://localhost:3000/users/101
Completed 302 Found in 6ms (ActiveRecord: 0.3ms)
Started GET "/users/101" for 127.0.0.1 at 2013-04-11 14:31:12 +0200
Processing by UsersController#show as HTML
Parameters: {"id"=>"101"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", "101"]]
(0.3ms) SELECT COUNT(*) FROM "scaffolds" WHERE "scaffolds"."user_id" = 101
CACHE (0.0ms) SELECT COUNT(*) FROM "scaffolds" WHERE "scaffolds"."user_id" = 101
Scaffold Load (0.3ms) SELECT "scaffolds".* FROM "scaffolds" WHERE "scaffolds"."user_id" = 101 LIMIT 30 OFFSET 0
Rendered scaffolds/_scaffold.html.erb (0.5ms)
Rendered users/show.html.erb within layouts/application (4.5ms)
Rendered layouts/_shim.html.erb (0.0ms)
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'UiqM3lDjBQgoJqSc8P_apQ' LIMIT 1
Rendered layouts/_header.html.erb (2.3ms)
Rendered layouts/_footer.html.erb (0.9ms)
Completed 200 OK in 33ms (Views: 28.6ms | ActiveRecord: 0.9ms)
Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2013-04-11 14:31:13 +0200
Served asset /application.css - 304 Not Modified (17ms)
Started GET "/assets/custom.css?body=1" for 127.0.0.1 at 2013-04-11 14:31:13 +0200
Served asset /custom.css - 304 Not Modified (1ms)
Started GET "/assets/sessions.css?body=1" for 127.0.0.1 at 2013-04-11 14:31:13 +0200
Served asset /sessions.css - 304 Not Modified (0ms)
Started GET "/assets/static_pages.css?body=1" for 127.0.0.1 at 2013-04-11 14:31:13 +0200
Served asset /static_pages.css - 304 Not Modified (0ms)
Started GET "/assets/users.css?body=1" for 127.0.0.1 at 2013-04-11 14:31:13 +0200
Served asset /users.css - 304 Not Modified (0ms)
Started GET "/assets/tracks.css?body=1" for 127.0.0.1 at 2013-04-11 14:31:13 +0200
Served asset /tracks.css - 304 Not Modified (0ms)
Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2013-04-11 14:31:13 +0200
Served asset /jquery.js - 304 Not Modified (0ms)
Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2013-04-11 14:31:13 +0200
Served asset /jquery_ujs.js - 304 Not Modified (0ms)
Started GET "/assets/bootstrap-affix.js?body=1" for 127.0.0.1 at 2013-04-11 14:31:13 +0200
Served asset /bootstrap-affix.js - 304 Not Modified (0ms)
Started GET "/assets/bootstrap-transition.js?body=1" for 127.0.0.1 at 2013-04-11 14:31:13 +0200
Served asset /bootstrap-transition.js - 304 Not Modified (0ms)
Started GET "/assets/bootstrap-carousel.js?body=1" for 127.0.0.1 at 2013-04-11 14:31:13 +0200
Served asset /bootstrap-carousel.js - 304 Not Modified (0ms)
Started GET "/assets/bootstrap-button.js?body=1" for 127.0.0.1 at 2013-04-11 14:31:13 +0200
Served asset /bootstrap-button.js - 304 Not Modified (0ms)
Started GET "/assets/bootstrap-alert.js?body=1" for 127.0.0.1 at 2013-04-11 14:31:13 +0200
Served asset /bootstrap-alert.js - 304 Not Modified (0ms)
Started GET "/assets/bootstrap-collapse.js?body=1" for 127.0.0.1 at 2013-04-11 14:31:13 +0200
Served asset /bootstrap-collapse.js - 304 Not Modified (0ms)
Started GET "/assets/bootstrap-modal.js?body=1" for 127.0.0.1 at 2013-04-11 14:31:13 +0200
Served asset /bootstrap-modal.js - 304 Not Modified (0ms)
Started GET "/assets/bootstrap-dropdown.js?body=1" for 127.0.0.1 at 2013-04-11 14:31:13 +0200
Served asset /bootstrap-dropdown.js - 304 Not Modified (0ms)
Started GET "/assets/bootstrap-tab.js?body=1" for 127.0.0.1 at 2013-04-11 14:31:13 +0200
Served asset /bootstrap-tab.js - 304 Not Modified (0ms)
Started GET "/assets/bootstrap-tooltip.js?body=1" for 127.0.0.1 at 2013-04-11 14:31:13 +0200
Served asset /bootstrap-tooltip.js - 304 Not Modified (0ms)
Started GET "/assets/bootstrap-popover.js?body=1" for 127.0.0.1 at 2013-04-11 14:31:13 +0200
Served asset /bootstrap-popover.js - 304 Not Modified (0ms)
Started GET "/assets/bootstrap-typeahead.js?body=1" for 127.0.0.1 at 2013-04-11 14:31:13 +0200
Served asset /bootstrap-typeahead.js - 304 Not Modified (0ms)
Started GET "/assets/bootstrap.js?body=1" for 127.0.0.1 at 2013-04-11 14:31:13 +0200
Served asset /bootstrap.js - 304 Not Modified (0ms)
Started GET "/assets/users.js?body=1" for 127.0.0.1 at 2013-04-11 14:31:13 +0200
Served asset /users.js - 304 Not Modified (0ms)
Started GET "/assets/sessions.js?body=1" for 127.0.0.1 at 2013-04-11 14:31:13 +0200
Served asset /sessions.js - 304 Not Modified (0ms)
Started GET "/assets/static_pages.js?body=1" for 127.0.0.1 at 2013-04-11 14:31:13 +0200
Served asset /static_pages.js - 304 Not Modified (0ms)
Started GET "/assets/tracks.js?body=1" for 127.0.0.1 at 2013-04-11 14:31:13 +0200
Served asset /tracks.js - 304 Not Modified (0ms)
Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2013-04-11 14:31:13 +0200
Served asset /application.js - 304 Not Modified (1ms)
Started GET "/assets/bootstrap-scrollspy.js?body=1" for 127.0.0.1 at 2013-04-11 14:31:13 +0200
Served asset /bootstrap-scrollspy.js - 304 Not Modified (0ms)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment