Add interface to create and update user accounts
This commit is contained in:
parent
5c89cf1501
commit
5f3713aecd
11 changed files with 100 additions and 1 deletions
3
app/assets/stylesheets/users.scss
Normal file
3
app/assets/stylesheets/users.scss
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
// Place all the styles related to the Users controller here.
|
||||||
|
// They will automatically be included in application.css.
|
||||||
|
// You can use Sass (SCSS) here: https://sass-lang.com/
|
55
app/controllers/users_controller.rb
Normal file
55
app/controllers/users_controller.rb
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
class UsersController < ApplicationController
|
||||||
|
def new
|
||||||
|
@user=User.new
|
||||||
|
end
|
||||||
|
|
||||||
|
def create
|
||||||
|
@user=User.new(user_params)
|
||||||
|
if @user.save
|
||||||
|
render 'show'
|
||||||
|
else
|
||||||
|
redirect_to new_user_path
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def edit
|
||||||
|
@user = User.find(params[:id])
|
||||||
|
end
|
||||||
|
|
||||||
|
def update
|
||||||
|
@user = User.find(params[:id])
|
||||||
|
attrs = user_params
|
||||||
|
respond_to do |format|
|
||||||
|
if @user.update(attrs)
|
||||||
|
flash[:notice] = 'Konto wurde erfolgreich aktualisiert.'
|
||||||
|
format.html { redirect_to user_url(@user) }
|
||||||
|
format.xml { head :ok }
|
||||||
|
else
|
||||||
|
format.html { render :action => "edit" }
|
||||||
|
format.xml { render :xml => @user.errors.to_xml }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def show
|
||||||
|
@user = User.find_by_id(params[:id])
|
||||||
|
end
|
||||||
|
|
||||||
|
def index
|
||||||
|
@users = User.all
|
||||||
|
end
|
||||||
|
|
||||||
|
def destroy
|
||||||
|
@user = User.find(params[:id])
|
||||||
|
@user.destroy
|
||||||
|
respond_to do |format|
|
||||||
|
format.html { redirect_to users_url }
|
||||||
|
format.xml { head :ok }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def user_params
|
||||||
|
params.require(:user).permit(:email, :password)
|
||||||
|
end
|
||||||
|
end
|
2
app/helpers/users_helper.rb
Normal file
2
app/helpers/users_helper.rb
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
module UsersHelper
|
||||||
|
end
|
|
@ -3,6 +3,7 @@ class User < ApplicationRecord
|
||||||
has_many :user_accesses, :dependent => :destroy
|
has_many :user_accesses, :dependent => :destroy
|
||||||
has_many :suppliers, :through => :user_accesses
|
has_many :suppliers, :through => :user_accesses
|
||||||
|
|
||||||
|
|
||||||
attr_accessor :password
|
attr_accessor :password
|
||||||
before_save :encrypt_password
|
before_save :encrypt_password
|
||||||
|
|
||||||
|
|
|
@ -20,4 +20,6 @@
|
||||||
|
|
||||||
%br/
|
%br/
|
||||||
= link_to 'Neuer Lieferant', new_supplier_url
|
= link_to 'Neuer Lieferant', new_supplier_url
|
||||||
|
|
|
||||||
|
= link_to 'Konten', users_url
|
||||||
|
|
9
app/views/users/_form.haml
Normal file
9
app/views/users/_form.haml
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
= simple_form_for @user do |f|
|
||||||
|
= f.input :email, required: true
|
||||||
|
= f.input :password, required: true
|
||||||
|
= f.input :password_confirmation, required: true
|
||||||
|
|
||||||
|
.form-actions
|
||||||
|
= f.submit class: 'btn'
|
||||||
|
|
||||||
|
= link_to 'Zurück', users_path
|
2
app/views/users/edit.haml
Normal file
2
app/views/users/edit.haml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
%h1 Konto bearbeiten
|
||||||
|
= render 'form'
|
8
app/views/users/index.html.haml
Normal file
8
app/views/users/index.html.haml
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
%h1 Konten
|
||||||
|
- for user in @users
|
||||||
|
%li
|
||||||
|
= link_to user.email, user_path(user)
|
||||||
|
%br
|
||||||
|
= link_to 'Neues Konto', new_user_url
|
||||||
|
|
|
||||||
|
= link_to 'Zurück', suppliers_path
|
2
app/views/users/new.html.haml
Normal file
2
app/views/users/new.html.haml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
%h1 Neues Konto anlegen
|
||||||
|
= render 'form'
|
9
app/views/users/show.html.haml
Normal file
9
app/views/users/show.html.haml
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
%h1 Konto bearbeiten
|
||||||
|
= @user.email
|
||||||
|
%br
|
||||||
|
%br
|
||||||
|
= link_to 'Bearbeiten', edit_user_path(@user)
|
||||||
|
|
|
||||||
|
= link_to 'Löschen', user_path(@user), data: { confirm: 'Bist du sicher?' }, :method => :delete
|
||||||
|
|
|
||||||
|
=link_to 'Zurück', users_path
|
|
@ -1,4 +1,6 @@
|
||||||
Rails.application.routes.draw do
|
Rails.application.routes.draw do
|
||||||
|
get 'users/new'
|
||||||
|
get 'users/show'
|
||||||
get 'log_in' => 'sessions#new', :as => :log_in
|
get 'log_in' => 'sessions#new', :as => :log_in
|
||||||
match 'log_out' => 'sessions#destroy', :as => :log_out, :via => [:get, :post]
|
match 'log_out' => 'sessions#destroy', :as => :log_out, :via => [:get, :post]
|
||||||
resources :sessions, :only => [:new, :create, :destroy]
|
resources :sessions, :only => [:new, :create, :destroy]
|
||||||
|
@ -15,5 +17,9 @@ Rails.application.routes.draw do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
resources :users
|
||||||
|
|
||||||
match '/:controller(/:action(/:id))', :via => [:get, :post]
|
match '/:controller(/:action(/:id))', :via => [:get, :post]
|
||||||
|
match '/users', to: 'users#index', via: 'get'
|
||||||
|
match '/users/:id', to: 'users#show', via: 'get'
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue