Skip to content

Instantly share code, notes, and snippets.

@Veske
Last active December 28, 2015 17:09
Show Gist options
  • Save Veske/7533950 to your computer and use it in GitHub Desktop.
Save Veske/7533950 to your computer and use it in GitHub Desktop.
User creation
irb(main):001:0> User.create(name: "dudu", email: "dudu@dudu.ee", password: "1234", password_confirmation: "1234")
(1.0ms) begin transaction
User Exists (0.0ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('dudu@dudu.ee') LIMIT 1
Binary data inserted for `string` type on column `password_digest`
SQL (4.0ms) INSERT INTO "users" ("created_at", "email", "name", "password_digest", "updated_at") VALUES (?, ?, ?, ?
[["created_at", Mon, 18 Nov 2013 20:50:04 UTC +00:00], ["email", "dudu@dudu.ee"], ["name", "dudu"], ["password_digest
2a$10$ENowSeWI6nGZbVIHVUBfuuwKEWeZ9I81Ci/Nru2Q6Q0.paUi7kFXu"], ["updated_at", Mon, 18 Nov 2013 20:50:04 UTC +00:00]]
(74.1ms) commit transaction
=> #<User id: 5, name: "dudu", email: "dudu@dudu.ee", created_at: "2013-11-18 20:50:04", updated_at: "2013-11-18 20:50
password_digest: "$2a$10$ENowSeWI6nGZbVIHVUBfuuwKEWeZ9I81Ci/Nru2Q6Q0....">
irb(main):002:0>
ActiveRecord::Schema.define(version: 20131106101452) do
create_table "users", force: true do |t|
t.string "name"
t.string "email"
t.datetime "created_at"
t.datetime "updated_at"
t.string "password_digest"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
end
<% provide(:title, "Create an account") %>
<p class="content">
<p class="text">
Account creation</br>
Fill out the forms below
</p>
<%= form_for(@user) do |f| %>
<div class="forms">
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :name %>
<%= f.text_field :name%>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Register my Account"%>
</div>
<% end %>
Started POST "/users" for 127.0.0.1 at 2013-11-18 22:50:55 +0200
ActiveRecord::SchemaMigration Load (0.0ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by UsersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"tOZkHqPqhlJdgqpEZD7q2R1NvsfKAK9Bwum+I+LcJOI=", "user"=>{"email"=>"asdasd@asd.ee", "name"=>"asdasd"
, "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Register my Account"}
(0.0ms) begin transaction
User Exists (1.0ms) SELECT 1 AS one FROM "users" WHERE "users"."email" IS NULL LIMIT 1
(0.0ms) rollback transaction
Rendered users/new.html.erb within layouts/application (7.0ms)
Rendered layouts/_menu.html.erb (0.0ms)
Rendered layouts/_footer.html.erb (0.0ms)
Completed 200 OK in 110ms (Views: 54.0ms | ActiveRecord: 1.0ms)
class User < ActiveRecord::Base
has_secure_password
before_save { self.email = email.downcase }
validates :name, presence: true, length: {maximum: 50}
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX}, uniqueness: {case_sensitive: false}
validates :password, length: {minimum: 3}
end
class UsersController < ApplicationController
def new
@user = User.new
end
def create
@user = User.new(params[user_params])
if @user.save
sign_in @user
flash[:success]= "Registration success!"
redirect_to @user
else
render 'new'
end
end
def show
if signed_in?
@user = User.find(params[:id])
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment