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
end
include OrdersHelper
include ApplicationHelper
include OrdersHelper
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

View file

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

View file

@ -23,6 +23,7 @@
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:rememberable, :trackable
has_attached_file :avatar, styles: { medium: "100x100>" }, default_style: :medium
has_many :orders, -> { includes :products }
belongs_to :dagschotel, class_name: 'Product'
@ -31,6 +32,7 @@ class User < ActiveRecord::Base
validates :name, presence: true
validates :last_name, presence: true
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
"#{name} #{last_name}"

View file

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

View file

@ -10,9 +10,11 @@
<%= form_password_field f, :password %>
<%= form_password_field f, :password_confirmation %>
<div class="actions">
<%= f.submit "Sign up", class: 'btn btn-primary' %>
</div>
<%= f.label :avatar %>
<%= f.file_field :avatar %>
<br />
<%= f.submit "Sign up", class: 'btn btn-primary' %>
<% end %>
<%= 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>
<%= render partial: 'flash' %>
<ul class="users">
<% @users.each do |user| %>
<li>
<%= link_to user.name, user %> |
<%= link_to "delete", user, method: :delete,
data: { confirm: "You sure?" } %>
</li>
<% end %>
</ul>
<div class="row">
<div class="col-md-12">
<%= render @users %>
</div>
</div>

View file

@ -4,64 +4,9 @@ Rails.application.routes.draw do
devise_for :users
resources :users, only: [:show, :index] do
resources :users do
resources :orders, only: [:new, :create, :index]
end
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

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.
ActiveRecord::Schema.define(version: 20141209192545) do
ActiveRecord::Schema.define(version: 20141209204351) do
create_table "order_products", force: true do |t|
t.integer "order_id"
@ -56,6 +56,10 @@ ActiveRecord::Schema.define(version: 20141209192545) do
t.string "last_sign_in_ip"
t.boolean "admin"
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

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