Add some validations to User and errors to form
This commit is contained in:
parent
b6cba92124
commit
b423a9d0e8
5 changed files with 19 additions and 7 deletions
|
@ -1,13 +1,13 @@
|
|||
class User < ActiveRecord::Base
|
||||
has_many :orders
|
||||
before_save :init
|
||||
|
||||
validates :name, presence: true
|
||||
validates :last_name, presence: true
|
||||
validates :nickname, presence: true, uniqueness: true
|
||||
|
||||
|
||||
def init
|
||||
self.balance ||= 0
|
||||
def full_name
|
||||
"#{name} #{last_name}"
|
||||
end
|
||||
|
||||
|
||||
has_secure_password
|
||||
end
|
||||
|
|
5
app/views/application/_errors.html.erb
Normal file
5
app/views/application/_errors.html.erb
Normal file
|
@ -0,0 +1,5 @@
|
|||
<ul>
|
||||
<% model.errors.full_messages.each do |e| %>
|
||||
<%= content_tag(:li, e) %>
|
||||
<% end %>
|
||||
</ul>
|
|
@ -3,6 +3,8 @@
|
|||
<div class="row">
|
||||
<div class="col-md-6 col-md-offset-3">
|
||||
<%= form_for(@user) do |f| %>
|
||||
<%= render partial: 'errors', locals: { model: @user } %>
|
||||
|
||||
<%= f.label :nickname %>
|
||||
<%= f.text_field :nickname %>
|
||||
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
class AddDefaultValueBalanceToUsers < ActiveRecord::Migration
|
||||
def change
|
||||
change_column :users, :balance, :integer, default: 0
|
||||
end
|
||||
end
|
|
@ -11,7 +11,7 @@
|
|||
#
|
||||
# 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|
|
||||
t.integer "user_id"
|
||||
|
@ -36,7 +36,7 @@ ActiveRecord::Schema.define(version: 20141124091707) do
|
|||
create_table "users", force: true do |t|
|
||||
t.string "name"
|
||||
t.string "last_name"
|
||||
t.integer "balance"
|
||||
t.integer "balance", default: 0
|
||||
t.string "nickname"
|
||||
t.string "password_digest"
|
||||
t.datetime "created_at"
|
||||
|
|
Loading…
Reference in a new issue