Add some validations to User and errors to form

This commit is contained in:
Benjamin Cousaert 2014-11-25 11:30:55 +01:00
parent b6cba92124
commit b423a9d0e8
5 changed files with 19 additions and 7 deletions

View file

@ -1,13 +1,13 @@
class User < ActiveRecord::Base class User < ActiveRecord::Base
has_many :orders has_many :orders
before_save :init
validates :name, presence: true
validates :last_name, presence: true
validates :nickname, presence: true, uniqueness: true
def full_name
def init "#{name} #{last_name}"
self.balance ||= 0
end end
has_secure_password has_secure_password
end end

View file

@ -0,0 +1,5 @@
<ul>
<% model.errors.full_messages.each do |e| %>
<%= content_tag(:li, e) %>
<% end %>
</ul>

View file

@ -3,6 +3,8 @@
<div class="row"> <div class="row">
<div class="col-md-6 col-md-offset-3"> <div class="col-md-6 col-md-offset-3">
<%= form_for(@user) do |f| %> <%= form_for(@user) do |f| %>
<%= render partial: 'errors', locals: { model: @user } %>
<%= f.label :nickname %> <%= f.label :nickname %>
<%= f.text_field :nickname %> <%= f.text_field :nickname %>

View file

@ -0,0 +1,5 @@
class AddDefaultValueBalanceToUsers < ActiveRecord::Migration
def change
change_column :users, :balance, :integer, default: 0
end
end

View file

@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20141124091707) do ActiveRecord::Schema.define(version: 20141125102501) do
create_table "orders", force: true do |t| create_table "orders", force: true do |t|
t.integer "user_id" t.integer "user_id"
@ -36,7 +36,7 @@ ActiveRecord::Schema.define(version: 20141124091707) do
create_table "users", force: true do |t| create_table "users", force: true do |t|
t.string "name" t.string "name"
t.string "last_name" t.string "last_name"
t.integer "balance" t.integer "balance", default: 0
t.string "nickname" t.string "nickname"
t.string "password_digest" t.string "password_digest"
t.datetime "created_at" t.datetime "created_at"