diff --git a/app/models/user.rb b/app/models/user.rb index 3c791bb..34ad658 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/views/application/_errors.html.erb b/app/views/application/_errors.html.erb new file mode 100644 index 0000000..d7c0fb2 --- /dev/null +++ b/app/views/application/_errors.html.erb @@ -0,0 +1,5 @@ + diff --git a/app/views/users/new.html.erb b/app/views/users/new.html.erb index 6fa72e3..dfe2528 100644 --- a/app/views/users/new.html.erb +++ b/app/views/users/new.html.erb @@ -3,6 +3,8 @@
<%= form_for(@user) do |f| %> + <%= render partial: 'errors', locals: { model: @user } %> + <%= f.label :nickname %> <%= f.text_field :nickname %> diff --git a/db/migrate/20141125102501_add_default_value_balance_to_users.rb b/db/migrate/20141125102501_add_default_value_balance_to_users.rb new file mode 100644 index 0000000..d0e7afd --- /dev/null +++ b/db/migrate/20141125102501_add_default_value_balance_to_users.rb @@ -0,0 +1,5 @@ +class AddDefaultValueBalanceToUsers < ActiveRecord::Migration + def change + change_column :users, :balance, :integer, default: 0 + end +end diff --git a/db/schema.rb b/db/schema.rb index 9484f1a..d2a68be 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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"