Add avatar to users

This commit is contained in:
Tom Naessens 2014-12-09 22:32:54 +01:00
parent 07966e550d
commit 4ecfac5070
14 changed files with 63 additions and 77 deletions

View file

@ -12,14 +12,14 @@ class ApplicationController < ActionController::Base
new_user_session_path new_user_session_path
end end
include OrdersHelper include OrdersHelper
include ApplicationHelper include ApplicationHelper
protected protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:nickname, :name, :last_name, :password, :password_confirmation, :current_password, :avatar) }
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:nickname, :name, :last_name, :password, :password_confirmation, :current_password, :avatar) }
end
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(
:nickname, :name, :last_name, :password, :password_confirmation
) }
end
end end

View file

@ -7,4 +7,11 @@ class UsersController < ApplicationController
def index def index
@users = User.all @users = User.all
end end
def destroy
User.find(params[:id]).destroy
flash[:success] = "Succesfully removed user"
redirect_to users_path
end
end end

View file

@ -23,6 +23,7 @@
class User < ActiveRecord::Base class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, devise :database_authenticatable, :registerable,
:rememberable, :trackable :rememberable, :trackable
has_attached_file :avatar, styles: { medium: "100x100>" }, default_style: :medium
has_many :orders, -> { includes :products } has_many :orders, -> { includes :products }
belongs_to :dagschotel, class_name: 'Product' belongs_to :dagschotel, class_name: 'Product'
@ -31,6 +32,7 @@ class User < ActiveRecord::Base
validates :name, presence: true validates :name, presence: true
validates :last_name, presence: true validates :last_name, presence: true
validates :password, length: { in: 8..128 }, confirmation: true, on: :create validates :password, length: { in: 8..128 }, confirmation: true, on: :create
validates_attachment :avatar, presence: true, content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }
def full_name def full_name
"#{name} #{last_name}" "#{name} #{last_name}"

View file

@ -12,5 +12,8 @@
<%= form_password_field f, :current_password %> <%= form_password_field f, :current_password %>
<%= f.label :avatar %>
<%= f.file_field :avatar %>
<%= f.submit "Update", class: 'btn btn-primary' %> <%= f.submit "Update", class: 'btn btn-primary' %>
<% end %> <% end %>

View file

@ -10,9 +10,11 @@
<%= form_password_field f, :password %> <%= form_password_field f, :password %>
<%= form_password_field f, :password_confirmation %> <%= form_password_field f, :password_confirmation %>
<div class="actions"> <%= f.label :avatar %>
<%= f.submit "Sign up", class: 'btn btn-primary' %> <%= f.file_field :avatar %>
</div>
<br />
<%= f.submit "Sign up", class: 'btn btn-primary' %>
<% end %> <% end %>
<%= render "devise/shared/links" %> <%= render "devise/shared/links" %>

View file

@ -0,0 +1,15 @@
<div class="col-md-3">
<div class="thumbnail">
<%= image_tag user.avatar if user.avatar.exists? %>
<div class="caption">
<h3><%= user.full_name %></h3>
<p><strong>Name:</strong> <%= user.name %></p>
<p><strong>Last name:</strong> <%= user.last_name %></p>
<p><strong>Nickname:</strong> <%= user.nickname %></p>
<p><strong>Balance:</strong> <%= user.balance %></p>
<p>
<%= link_to "Delete", user_path(user), method: :delete, class: "btn btn-danger", data: {confirm: 'Are you sure?'} %>
</p>
</div>
</div>
</div>

View file

@ -1,11 +1,8 @@
<h1>All users</h1> <h1>All users</h1>
<%= render partial: 'flash' %>
<ul class="users"> <div class="row">
<% @users.each do |user| %> <div class="col-md-12">
<li> <%= render @users %>
<%= link_to user.name, user %> | </div>
<%= link_to "delete", user, method: :delete, </div>
data: { confirm: "You sure?" } %>
</li>
<% end %>
</ul>

View file

@ -4,64 +4,9 @@ Rails.application.routes.draw do
devise_for :users devise_for :users
resources :users, only: [:show, :index] do resources :users do
resources :orders, only: [:new, :create, :index] resources :orders, only: [:new, :create, :index]
end end
resources :products resources :products
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
# root 'welcome#index'
# Example of regular route:
# get 'products/:id' => 'catalog#view'
# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
# Example resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Example resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Example resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Example resource route with more complex sub-resources:
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', on: :collection
# end
# end
# Example resource route with concerns:
# concern :toggleable do
# post 'toggle'
# end
# resources :posts, concerns: :toggleable
# resources :photos, concerns: :toggleable
# Example resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
end end

View file

@ -0,0 +1,11 @@
class AddAttachmentAvatarToUsers < ActiveRecord::Migration
def self.up
change_table :users do |t|
t.attachment :avatar
end
end
def self.down
remove_attachment :users, :avatar
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: 20141209192545) do ActiveRecord::Schema.define(version: 20141209204351) do
create_table "order_products", force: true do |t| create_table "order_products", force: true do |t|
t.integer "order_id" t.integer "order_id"
@ -56,6 +56,10 @@ ActiveRecord::Schema.define(version: 20141209192545) do
t.string "last_sign_in_ip" t.string "last_sign_in_ip"
t.boolean "admin" t.boolean "admin"
t.integer "dagschotel_id" t.integer "dagschotel_id"
t.string "avatar_file_name"
t.string "avatar_content_type"
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
end end
end end

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB