I'm new to Ruby on Rails and would appreciate any support!
Users can create a case and select a specific diagnosis via dropdown. The Admin (called 'rki') can see a list of all diagnoses in the database. Now I'm trying to implement that the admin can choose a specific diagnosis und get a list of all cases, with that diagnosis.
This is my RkisController
class RkisController < ApplicationController
before_action :authenticate_user!
before_action :current_user_rki?
def current_user_rki?
return if current_user.role == 'rki'
redirect_to root_path
end
def index
@diagnoses = Diagnosis.all
end
def all_cases
#show all cases with a certain diagnosis
end
end
And this is my Model for Case
class Case < ApplicationRecord
belongs_to :user
belongs_to :diagnosis
belongs_to :district
end
Diagnosis
class Diagnosis < ApplicationRecord
has_many :cases
end
CasesController
class CasesController < ApplicationController
before_action :set_case, only: [:show, :edit, :update, :destroy, :confirm]
def index
@cases = current_user.cases
end
def show
end
def new
@case = Case.new
@case.user_id = current_user.id
end
def edit
end
def create
@case = Case.new(case_params) do |c|
c.user_id = current_user.id
end
@case.district = current_user.district
respond_to do |format|
if @case.save
format.html { redirect_to @case, notice: 'Case was successfully created.' }
format.json { render :show, status: :created, location: @case }
else
format.html { render :new }
format.json { render json: @case.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @case.update(case_params)
format.html { redirect_to @case, notice: 'Case was successfully updated.' }
format.json { render :show, status: :ok, location: @case }
else
format.html { render :edit }
format.json { render json: @case.errors, status: :unprocessable_entity }
end
end
end
def destroy
@case.destroy
respond_to do |format|
format.html { redirect_to cases_url, notice: 'Case was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def allowed_to_create
redirect_to root_path unless current_user.role.in?(['arzt', 'labor'])
end
def set_case
@case = Case.find(params[:id])
end
def case_params
params.require(:case).permit(:first_name, :last_name, :gender,:birthdate, :place_of_residence,
:diagnosis_id, :user_id, :case_id, :confirmed_at, :district_id)
end
end
DiagnosisController
class DiagnosesController < ApplicationController
before_action :set_diagnosis, only: [:show, :edit, :update, :destroy]
def index
@diagnoses = Diagnosis.all
end
def show
end
def new
@diagnosis = Diagnosis.new
end
def edit
end
def create
@diagnosis = Diagnosis.new(diagnosis_params)
respond_to do |format|
if @diagnosis.save
format.html { redirect_to @diagnosis, notice: 'Diagnosis was successfully created.' }
format.json { render :show, status: :created, location: @diagnosis }
else
format.html { render :new }
format.json { render json: @diagnosis.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @diagnosis.update(diagnosis_params)
format.html { redirect_to @diagnosis, notice: 'Diagnosis was successfully updated.' }
format.json { render :show, status: :ok, location: @diagnosis }
else
format.html { render :edit }
format.json { render json: @diagnosis.errors, status: :unprocessable_entity }
end
end
end
def destroy
@diagnosis.destroy
respond_to do |format|
format.html { redirect_to diagnoses_url, notice: 'Diagnosis was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_diagnosis
@diagnosis = Diagnosis.find(params[:id])
end
def diagnosis_params
params.require(:diagnosis).permit(:illness)
end
end
Thank you very much in advance.