Saving state of object if changes are made - Rails - ruby-on-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?

Related

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

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

Rails, update the product right after create one

I have the web app on RoR, but the issue is once the user upload the image, the product_id is not associated with the product_attachments. Not until I proceed to next form.
Following were my controller
ProductAttachmentsController:
class ProductAttachmentsController < ApplicationController
before_action :set_product_attachment, only: [:show, :edit, :update, :destroy]
def index
#product_attachments = ProductAttachment.all
end
def show
end
def new
#product_attachment = ProductAttachment.new
end
def create
#product_attachment = ProductAttachment.new(product_attachment_params)
respond_to do |format|
if #product_attachment.save
format.html { redirect_to #product_attachment, notice: 'Product attachment was successfully created.' }
format.json {render :json => #product_attachment}
else
format.html { render :new }
format.json { render json: #product_attachment.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #product_attachment.update(product_attachment_params)
format.html { redirect_to #product_attachment.product, notice: 'Product attachment was successfully updated.' }
format.json { render :show, status: :ok, location: #product_attachment }
else
format.html { render :edit }
format.json { render json: #product_attachment.errors, status: :unprocessable_entity }
end
end
end
def destroy
#product_attachment.destroy
respond_to do |format|
format.html { redirect_to product_attachments_url, notice: 'Product attachment was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_product_attachment
#product_attachment = ProductAttachment.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def product_attachment_params
params.require(:product_attachment).permit(:product_id, :attachment)
end
end
How can I trick the create method, so it will create a product_id when I create product_attachment? Currently I need to proceed next step and it trigger update method to insert product_id in product_attachments table. Thanks!!
You can store the product attachment id in a session variable
if #product_attachment.save
session[:product_attachment] = #product_attachment.id ### HERE!
format.html { redirect_to #product_attachment, notice: 'Product attachment was successfully created.' }
format.json {render :json => #product_attachment}
else
format.html { render :new }
format.json { render json: #product_attachment.errors, status: :unprocessable_entity }
end
When you create the product (in the ProductsController ) recall the attachment and update the product_id
if #product.save
if session[:product_attachment]
ProductAttachment.find(session[:product_attachment]).update_attribute(:product_id, #product.id)
session[:product_attachment] = nil
end
...
end

PDF upload Ruby on Rails server - DB Postgres

Hello I am trying to store a PDF document in a PostgreSQL database using Ruby Rails.
Currently my code looks like this:
DB File:
'$20151126021922_create_pdf_creates.rb'
class CreatePdfCreates < ActiveRecord::Migration
def change
create_table :pdf_creates do |t|
t.binary :pdfload
t.timestamps null: false
end
end
end
Model:
'$pdf_create.rb'
class PdfCreate < ActiveRecord::Base
end
Controller:
'$pdf_creates_controller.rb'
class PdfCreatesController < ApplicationController
before_action :set_pdf_create, only: [:show, :edit, :update, :destroy]
# GET /pdf_creates
# GET /pdf_creates.json
def index
#pdf_creates = PdfCreate.all
end
# GET /pdf_creates/1
# GET /pdf_creates/1.json
def show
end
# GET /pdf_creates/new
def new
#pdf_create = PdfCreate.new
end
# GET /pdf_creates/1/edit
def edit
end
# POST /pdf_creates
# POST /pdf_creates.json
def newpdf
#pdf_create = PdfCreate.new(pdf_create_params)
respond_to do |format|
if #pdf_create.save
format.html { redirect_to #pdf_create, notice: 'Pdf create was successfully created.' }
format.json { render :show, status: :created, location: #pdf_create }
else
format.html { render :new }
format.json { render json: #pdf_create.errors, status: :unprocessable_entity }
end
end
end
def create
#pdf_create = PdfCreate.new(pdf_create_params)
#data = File.read(Rails.root + "tmp/consent(1).pdf")
#Document.create pdfload: data
respond_to do |format|
if #pdf_create.save
format.html { redirect_to #pdf_create, notice: 'Pdf create was successfully created.' }
format.json { render :show, status: :created, location: #pdf_create }
format.pdf { send_data #pdf_create.render}
else
format.html { render :new }
# format.json { render json: #pdf_create.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /pdf_creates/1
# PATCH/PUT /pdf_creates/1.json
def update
respond_to do |format|
if #pdf_create.update(pdf_create_params)
format.html { redirect_to #pdf_create, notice: 'Pdf create was successfully updated.' }
format.json { render :show, status: :ok, location: #pdf_create }
else
format.html { render :edit }
format.json { render json: #pdf_create.errors, status: :unprocessable_entity }
end
end
end
# DELETE /pdf_creates/1
# DELETE /pdf_creates/1.json
def destroy
#pdf_create.destroy
respond_to do |format|
format.html { redirect_to pdf_creates_url, notice: 'Pdf create was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_pdf_create
#pdf_create = PdfCreate.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def pdf_create_params
params.require(:pdf_create).permit(:pdfload)
end
end
I send a pdf form and the result that is returned is me:
{"id":5,"pdfload":null,"created_at":"2015-11-26T03:24:37.457Z","updated_at":"2015-11-26T03:24:37.457Z"}
What is wrong? Tks
You're sending the whole newly created record, not the PDF data stored in it.
Try changing this:
format.pdf { send_data #pdf_create.render}
to this:
format.pdf { send_data #pdf_create.pdfload}

How to remove respond_to block from scaffold_controller template

how to customize scaffold generator #was following this link
class IdeasController < ApplicationController
before_action :set_idea, only: [:show, :edit, :update, :destroy]
# GET /ideas
# GET /ideas.json
def index
#ideas = Idea.all
end
# GET /ideas/1
# GET /ideas/1.json
def show
end
# GET /ideas/new
def new
#idea = Idea.new
end
# GET /ideas/1/edit
def edit
end
# POST /ideas
# POST /ideas.json
def create
#idea = Idea.new(idea_params)
respond_to do |format|
if #idea.save
format.html { redirect_to #idea, notice: 'Idea was successfully created.' }
format.json { render action: 'show', status: :created, location: #idea }
else
format.html { render action: 'new' }
format.json { render json: #idea.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /ideas/1
# PATCH/PUT /ideas/1.json
def update
respond_to do |format|
if #idea.update(idea_params)
format.html { redirect_to #idea, notice: 'Idea was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #idea.errors, status: :unprocessable_entity }
end
end
end
# DELETE /ideas/1
# DELETE /ideas/1.json
def destroy
#idea.destroy
respond_to do |format|
format.html { redirect_to ideas_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_idea
#idea = Idea.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def idea_params
params.require(:idea).permit(:name, :description, :picture)
end
end
How to remove all the respond_to code ?
Use respond_with to make your controllers cleaner. This apidoc and
this screencast will answer all your related questions.
Your controller methods will be as clean as this:
def update
#idea.update(idea_params)
respond_with #idea, notice: 'Idea was successfully updated.'
end
To apply that to default scaffold controller template, just copy the template content from github and put it into RAILS_ROOT/lib/templates/rails/scaffold_controller/controller.rb. Then apply the respond_with approach there.
Just do so like this.
For e.g.
respond_to do |format|
if #idea.save
format.html { redirect_to #idea, notice: 'Idea was successfully created.' }
format.json { render action: 'show', status: :created, location: #idea }
else
format.html { render action: 'new' }
format.json { render json: #idea.errors, status: :unprocessable_entity }
end
end
can be replaced with
if #idea.save
redirect_to #idea, notice: 'Idea was successfully created.'
else
render 'new
end

Resources