Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
213 views
in Technique[技术] by (71.8m points)

ruby on rails - How can I show all cases, that belong to a certain diagnosis?

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Are you wanting to go to a show page from the index of diagnosis? If so you can just use the id from the index, passed to the show, ie normal flow. And then in your diagnosis show action you can have

def show
  @diagnosis = Diagnosis.includes(:cases).find_by(id: params[:id])
end

And then if using erb you can iterate through the cases

<% @diagnosis.cases.each do |case| %>
  <%= case.name %>
<% end %>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...