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

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.

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 %>

Related

Saving state of object if changes are made - Rails

Rails 7
I have the following:
controllers/orders_controller.rb
class OrdersController < ApplicationController
before_action :set_order, only: %i[ show edit update destroy ]
def index
#orders = Order.all
end
def show
end
def new
#order = Order.new
end
def edit
end
def create
#order = Order.new(order_params)
respond_to do |format|
if #order.save
format.html { redirect_to order_url(#order), notice: "Order was successfully created." }
format.json { render :show, status: :created, location: #order }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: #order.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #order.update(order_params)
format.html { redirect_to order_url(#order), notice: "Order was successfully updated." }
format.json { render :show, status: :ok, location: #order }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: #order.errors, status: :unprocessable_entity }
end
end
end
def destroy
#order.destroy
respond_to do |format|
format.html { redirect_to orders_url, notice: "Order was successfully destroyed." }
format.json { head :no_content }
end
end
private
def set_order
#order = Order.find(params[:id])
end
def order_params
params.require(:order).permit(:country, :sku, :abbreviation, :description)
end
end
models/order.rb
class Order < ApplicationRecord
end
What I would like to do, is create an OrderHistory Model, with the following fields:
order_id (int)
order_history (JSON)
When an update is made to the order, I would like the DB entry for the original order, saved as a JSON to OrderHistory.
What I am having trouble with, is creating a variable that contains the state of the existing order, when I enter the controller, without making it available to all app users. Any ideas?

Rails - NameError - uninitialized constant

I get for what is this error, but I can't see a mistake in my code.
A controller is plural, a model is singular and the table name is plural.
Error on visiting index:
NameError at /admin/custom_communities uninitialized constant
Admin::CustomCommunitiesController::CustomCommunity
Generated controller: (file: controllers/admin/custom_communities_controller.rb)
# frozen_string_literal: true
class Admin::CustomCommunitiesController < Admin::BaseController
before_action :set_custom_community, only: [:show, :edit, :update, :destroy]
def index
#custom_communities = CustomCommunity.page(params[:page])
end
def show; end
def new
#custom_community = CustomCommunity.new
end
def edit; end
def create
#custom_community = CustomCommunity.new(custom_community_params)
respond_to do |format|
if #custom_community.save
format.html { redirect_to #custom_community, notice: "Custom community was successfully created." }
format.json { render :show, status: :created, location: #custom_community }
else
format.html { render :new }
format.json { render json: #custom_community.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #custom_community.update(custom_community_params)
format.html { redirect_to #custom_community, notice: "Custom community was successfully updated." }
format.json { render :show, status: :ok, location: #custom_community }
else
format.html { render :edit }
format.json { render json: #custom_community.errors, status: :unprocessable_entity }
end
end
end
def destroy
#custom_community.destroy
respond_to do |format|
format.html { redirect_to admin_custom_communities_url, notice: "Custom community was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_custom_community
#custom_community = CustomCommunity.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def custom_community_params
params.require(:custom_community).permit(:name, :description, :picture, :should_delete_picture)
end
end
Model: (file: models/custom_community.rb)
# frozen_string_literal: true
class CustomCommunity < ApplicationRecord
end
Migration:
class CreateCustomCommunities < ActiveRecord::Migration[5.2]
def change
create_table :custom_communities do |t|
t.string :name
t.text :description
t.timestamps
end
end
end
Routes:
in admin routes:
resources :custom_communities
I'm sure someone else can explain why, but there are times rails will think that the following class in the index method:
class Admin::CustomCommunitiesController < Admin::BaseController
def index
CustomCommunity.all
end
end
Is referencing a class defined within the Admin::CustomCommunitiesController, i.e. it thinks you're trying to call Admin::CustomCommunitiesController::CustomCommunity.all. To 'unnamespace' the class, try:
# frozen_string_literal: true
class Admin::CustomCommunitiesController < Admin::BaseController
before_action :set_custom_community, only: [:show, :edit, :update, :destroy]
def index
#custom_communities = ::CustomCommunity.page(params[:page])
end
def show; end
def new
#custom_community = ::CustomCommunity.new
end
def edit; end
def create
#custom_community = ::CustomCommunity.new(custom_community_params)
respond_to do |format|
if #custom_community.save
format.html { redirect_to #custom_community, notice: "Custom community was successfully created." }
format.json { render :show, status: :created, location: #custom_community }
else
format.html { render :new }
format.json { render json: #custom_community.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #custom_community.update(custom_community_params)
format.html { redirect_to #custom_community, notice: "Custom community was successfully updated." }
format.json { render :show, status: :ok, location: #custom_community }
else
format.html { render :edit }
format.json { render json: #custom_community.errors, status: :unprocessable_entity }
end
end
end
def destroy
#custom_community.destroy
respond_to do |format|
format.html { redirect_to admin_custom_communities_url, notice: "Custom community was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_custom_community
#custom_community = ::CustomCommunity.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def custom_community_params
params.require(:custom_community).permit(:name, :description, :picture, :should_delete_picture)
end
end
I think that's all of them. Basically switch every CustomCommunity class call to ::CustomCommunity to unnamespace it.

SyntaxError in MusicsController#new

I want to Save Multiple Check box values to Database.When i run this code I got an error:- premature end of char-class: /[\][\]\"]/
and when I click on check box for selecting any check box value then it should display the details of that check box values below that page on only.
This is my code:
music.rb
class Music < ActiveRecord::Base
before_save do
self.genre.gsub!(/[\][\]\"]/,"") if attribute_present?("genre")
end
end
musics_controller.rb
class MusicsController < ApplicationController
before_action :set_music, only: [:show, :edit, :update, :destroy]
def index
#musics = Music.all
end
def show
end
def new
#music = Music.new
end
def edit
end
def create
#music = Music.new(music_params)
respond_to do |format|
if #music.save
format.html { redirect_to #music, notice: 'Music was successfully created.' }
format.json { render :show, status: :created, location: #music }
else
format.html { render :new }
format.json { render json: #music.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #music.update(music_params)
format.html { redirect_to #music, notice: 'Music was successfully updated.' }
format.json { render :show, status: :ok, location: #music }
else
format.html { render :edit }
format.json { render json: #music.errors, status: :unprocessable_entity }
end
end
end
def destroy
#music.destroy
respond_to do |format|
format.html { redirect_to musics_url, notice: 'Music was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_music
#music = Music.find(params[:id])
end
def music_params
params.require(:music).permit(:title, :artist, genre:[])
end
end

Merit doesn't add points to user after create action

I've used this instructions for simply add score when a user creates a "solucion" (which is a kind of "answer" to a micropost). I have added the has_merit line to user.rb (user model).
I want to display the user points earned for that action at the show view.
show.html.erb (for solucion):
<h2><span class="red"><%= current_user.points %></span><br>Points</br></h2>
It displays 0 points...
point_rules.rb:
module Merit
class PointRules
include Merit::PointRulesMethods
def initialize
score 5, on: 'solucions#create'
end
end
end
When I create a solucion with the current_user (already saving the user_id index and identifier to solucion), This is what my rails server output shows...
Direct link to github gist:
https://gist.github.com/roadev/7b34fd67ab93c979fa48
Embed:
<script src="https://gist.github.com/roadev/7b34fd67ab93c979fa48.js"></script>
EDIT:
solucions_micropost.rb
class SolucionsController < ApplicationController
before_action :set_solucion, only: [:show, :edit, :update, :destroy]
def index
#solucions = Solucion.all
end
def show
end
def new
#solucion = current_user.solucions.build
end
def edit
end
def create
#solucion = current_user.solucions.build(solucion_params)
respond_to do |format|
if #solucion.save
format.html { redirect_to #solucion, notice: 'Solucion was successfully created.' }
format.json { render action: 'show', status: :created, location: #solucion }
else
format.html { render action: 'new' }
format.json { render json: #solucion.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #solucion.update(solucion_params)
format.html { redirect_to #solucion, notice: 'Solucion was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #solucion.errors, status: :unprocessable_entity }
end
end
end
def destroy
#solucion.destroy
respond_to do |format|
format.html { redirect_to solucions_url }
format.json { head :no_content }
end
end
private
def set_solucion
#solucion = Solucion.find(params[:id])
end
def current_micropost
#solucion = microposts.find_by(id: params[:id])
end
def solucion_params
params.require(:solucion).permit(:solucion, :image, :micropost_id)
end
end
user.rb:
class User < ActiveRecord::Base
has_many :dreams
has_many :microposts
has_many :solucions
has_merit
end
I had a problem with a migration when I installed the merit gem.

undefined method using collect(&:foo).uniq

I am trying to show a collection of bookmark urls, catagorized by topic name for a current user. I am new to programming and I need help troubleshooting this error with #topics = #bookmarks.collect(&:topic).uniq causing the error:
undefined method `topic' for #<Bookmark:0xacfa42c>
Extracted source (around line #4):
def index
#bookmarks = current_user.bookmarks
#topics = #bookmarks.collect(&:topic).uniq
#liked_bookmarks = current_user.likes.collect(&:bookmark)
#liked_topics = #liked_bookmarks.collect(&:topic).uniq
end
app/controllers/user_bookmarks_controller.rb:4:in `index'
Here is my topic controller:
class TopicsController < ApplicationController
before_action :set_topic, only: [:show, :edit, :update, :destroy]
def index
#topics = Topic.all
end
def show
#bookmarks = #topic.bookmarks
end
def new
#topic = Topic.new
end
def edit
end
def create
#topic = Topic.new(topic_params)
respond_to do |format|
if #topic.save
format.html { redirect_to #topic, notice: 'Topic was successfully created.' }
format.json { render action: 'show', status: :created, location: #topic }
else
format.html { render action: 'new' }
format.json { render json: #topic.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #topic.update(topic_params)
format.html { redirect_to #topic, notice: 'Topic was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #topic.errors, status: :unprocessable_entity }
end
end
end
def destroy
#topic.destroy
respond_to do |format|
format.html { redirect_to topics_url }
format.json { head :no_content }
end
end
private
def set_topic
#topic = Topic.find(params[:id])
end
def topic_params
params.require(:topic).permit(:name)
end
end
Here is my bookmarks controller:
class BookmarksController < ApplicationController
before_action :set_bookmark, only: [:show, :edit, :update, :destroy]
def index
#bookmarks = Bookmark.all
end
def show
end
def new
#bookmark = Bookmark.new
end
def edit
end
def create
bookmark = Bookmark.where(url: params[:bookmark][:url]).first
#bookmark = bookmark.present? ? bookmark : Bookmark.new(bookmark_params)
if #bookmark.save
#bookmark.users << current_user
Rails.logger.info ">>>>>>>>>>>>> Bookmark: #{#bookmark.inspect}"
topic_names = params[:topic_names].split(' ')
topic_names.each do |topic_name|
name = topic_name.sub(/#/, '')
#bookmark.topics << Topic.find_or_create_by_name(name)
end
respond_to do |format|
format.html { redirect_to #bookmark, notice: 'Bookmark was successfully created.' }
format.json { render action: 'show', status: :created, location: #bookmark }
end
else
respond_to do |format|
format.html { render action: 'new' }
format.json { render json: #bookmark.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #bookmark.update(bookmark_params)
format.html { redirect_to #bookmark, notice: 'Bookmark was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #bookmark.errors, status: :unprocessable_entity }
end
end
end
def destroy
#bookmark.destroy
respond_to do |format|
format.html { redirect_to bookmarks_url }
format.json { head :no_content }
end
end
private
def set_bookmark
#bookmark = Bookmark.find(params[:id])
end
def bookmark_params
params.require(:bookmark).permit(:url)
end
end
Here is my incoming (bookmarks and topics) controller:
class BookmarksController < ApplicationController
before_action :set_bookmark, only: [:show, :edit, :update, :destroy]
def index
#bookmarks = Bookmark.all
end
def show
end
def new
#bookmark = Bookmark.new
end
def edit
end
def create
bookmark = Bookmark.where(url: params[:bookmark][:url]).first
#bookmark = bookmark.present? ? bookmark : Bookmark.new(bookmark_params)
if #bookmark.save
#bookmark.users << current_user
Rails.logger.info ">>>>>>>>>>>>> Bookmark: #{#bookmark.inspect}"
topic_names = params[:topic_names].split(' ')
topic_names.each do |topic_name|
name = topic_name.sub(/#/, '')
#bookmark.topics << Topic.find_or_create_by_name(name)
end
respond_to do |format|
format.html { redirect_to #bookmark, notice: 'Bookmark was successfully created.' }
format.json { render action: 'show', status: :created, location: #bookmark }
end
else
respond_to do |format|
format.html { render action: 'new' }
format.json { render json: #bookmark.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #bookmark.update(bookmark_params)
format.html { redirect_to #bookmark, notice: 'Bookmark was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #bookmark.errors, status: :unprocessable_entity }
end
end
end
def destroy
#bookmark.destroy
respond_to do |format|
format.html { redirect_to bookmarks_url }
format.json { head :no_content }
end
end
private
def set_bookmark
#bookmark = Bookmark.find(params[:id])
end
def bookmark_params
params.require(:bookmark).permit(:url)
end
end
Thanks in advance for your help!
Based on the info you've provided, it looks like your Bookmark model probably has many topics. A given Bookmark will have a method called topics but not topic. You may want to try something like:
#topics = #bookmarks.topics.uniq

Resources