Em Sta
Em Sta

Reputation: 1706

After save redirect to action "show"

I created an app where a Doctor can enter treatments for his patient. I added some validation and now when the doctor enters wrong things, Rails somehow redirects to the patient form and asks the user to change his input. But I don't want this behavior! When the input is false, Rails simply should not save it and redirect to patient show!!

Here is my controller:

def create
  @patient = Patient.find(params[:patient_id])
  @treatment = @patient.treatments.create(params[:treatment])
  redirect_to patient_path(@patient)
end

And my model:

class Treatment < ActiveRecord::Base
  belongs_to :patient
  validates :content, presence: true
  ...
end

Upvotes: 0

Views: 572

Answers (2)

Neeraj
Neeraj

Reputation: 180

You can do it by:

render action: 'show'

But if the patient is not created, how can we show it? You may want to use:

render action: 'new'

or

render action: 'index'

Upvotes: 0

Althaf Hameez
Althaf Hameez

Reputation: 1511

def create
  @patient = Patient.find(params[:patient_id])
  @treatment = @patient.treatments.new(params[:treatment])
  if @treatment.save
    redirect_to patient_path(@patient)
  else
    ## The page you want to redirect to
  end
end

Upvotes: 1

Related Questions