0

I have a controller

 def update
    @patient =  Patient.find(@score.patient_id)
    respond_to do |format|
      if @score.update(score_params)
      format.html {  redirect_to score_path(@score), notice: 'Score was successfully updated.' }
      format.json { render :show, status: :ok, location: @score }
  else
      format.html { render :edit }
      format.json { render json: @score.errors, status: :unprocessable_entity }
    end
   end
  end

When a "score" object is edited and passes validations everything is fine.

However (and here is the issue), in a sidebar I have a "current entries for this score" box which essentially shows the current entries for the object.It looks like this:

 <div class = "CompletionFrame">
   <div class="page-header">
   <h4>Your scores for this case</h4>
   </div>
 </div>

 <ol>
   <%= @score.exists(@score.dx1, @score.dxcon1) %>
   <%= @score.exists(@score.dx2, @score.dxcon2) %>
   <%= @score.exists(@score.dx3, @score.dxcon3) %>
   <%= @score.exists(@score.dx4, @score.dxcon4) %>
   <%= @score.exists(@score.dx5, @score.dxcon5) %>
</ol>

The details of the attributes are not important. What is strange is that if I attempt to update with invalid attributes, the controller renders the edit view but the invalid entries are shown in the "current entries for this score". If I go to another page and then back to this page, then the old (unedited) entries are shown. In other words, the invalid entries are not saved, but they are shown on the initial render :edit call by the controller.

My questions are:

Why is this?

How can I prevent this updating of the sidebar for that single "render"

I hope this makes sense.

1 Answer 1

2

The update method changes the local values of the instance variable object, and then tries to save it. If validation fails (because the local values don't pass validation) then the object isn't saved to the database but it still has the data sitting in it's own instance variables (which hold the field data). In other words, it's different to the version you would load out of the database.

When you do render, it renders out the page using the @score variable, which you've changed the local data for.

When you reload the page, the object is set by loading it from the database, and so gets the "old", valid, data.

Doing a render of the edit page when validation fails is useful because you can then display information about the fields which failed validation, in the form: for example, highlighting the problem fields and displaying some helpful feedback.

If your edit page is doing other stuff which relies on the object having valid data, then you should redirect to it rather than just render, which will make rails reload the object, OR save the failed object into a different variable to the one used elsewhere, so you can display the saved data on the page, and still have the failed object (with validation errors etc) in the form.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.