Add interface to create and update user accounts

This commit is contained in:
1resu 2021-12-19 04:13:05 +01:00
parent 5c89cf1501
commit 5f3713aecd
11 changed files with 100 additions and 1 deletions

View 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/

View 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

View File

@ -0,0 +1,2 @@
module UsersHelper
end

View File

@ -3,6 +3,7 @@ class User < ApplicationRecord
has_many :user_accesses, :dependent => :destroy
has_many :suppliers, :through => :user_accesses
attr_accessor :password
before_save :encrypt_password

View File

@ -20,4 +20,6 @@
%br/
= link_to 'Neuer Lieferant', new_supplier_url
|
= link_to 'Konten', users_url

View 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

View File

@ -0,0 +1,2 @@
%h1 Konto bearbeiten
= render 'form'

View 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

View File

@ -0,0 +1,2 @@
%h1 Neues Konto anlegen
= render 'form'

View 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

View File

@ -1,4 +1,6 @@
Rails.application.routes.draw do
get 'users/new'
get 'users/show'
get 'log_in' => 'sessions#new', :as => :log_in
match 'log_out' => 'sessions#destroy', :as => :log_out, :via => [:get, :post]
resources :sessions, :only => [:new, :create, :destroy]
@ -15,5 +17,9 @@ Rails.application.routes.draw do
end
end
resources :users
match '/:controller(/:action(/:id))', :via => [:get, :post]
match '/users', to: 'users#index', via: 'get'
match '/users/:id', to: 'users#show', via: 'get'
end