0

I'm trying to update a form that only updates the attribute set to false when not selected. They're currently defaulted to true (which is want I want) but if I click on one attribute it will update all of them to false. I only want to update only one in particular to be false, along with many other rows. I'm not sure how to do a check to say "if a selected attribute is chosen, update that one, else if the other attribute is selected update the other, and update all the collected/checked fields at once."

I've defined a collection in my routes:

resources :evidences do
      put :score, on: :collection
  end

This is my controller:

class EvidencesController < ApplicationController

  def score
    EvidenceScore.update_all({quality: false, alignment: false}, {id: params[:evidence_score_ids]} )
    redirect_to observation_domain_indicator_evidences_path
  end

end

This is my form:

<%= form_tag(score_observation_domain_indicator_evidences_path, :method => 'put') do %>>
        <tr>
          <th><h4> Evidence </h4></th>
          <th><h4> Quality </h4></th>
          <th><h4> Alignment</h4></th>
          <th><h4> Quality(Your Score) </h4></th>
          <th><h4> Alignment(Your Score) </h4></th>
        </tr>
      <% @indicator.evidence_scores.each do |evidence_score| %>
          <tr>
            <td><%= evidence_score.description %></td>
            <td><%= check_box_tag("evidence_score_ids[]", evidence_score.id) %></td>
            <td><%= check_box_tag("evidence_score_ids[]", evidence_score.id) %></td>
            <td><%= evidence_score.quality ? "Yes" : "No" %></td>
            <td><%= evidence_score.alignment ? "Yes" : "No" %></td>
          </tr>
        <% end %>
      </table>
    <%= submit_tag "Submit Scores" %>
<% end %>

As you click on the link below to a picture, on the second row, if I only select one of the checkboxes, both of them will update. I'm trying to have the ability where only one updates if I select one checkbox. By clicking on a checkbox and submitting it, it should render "No"

Here's a picture of what's happening.

https://www.dropbox.com/s/bhf1el4xdsrcox3/Screenshot%202014-03-02%2002.03.40.png

gary1410
  • 309
  • 1
  • 5
  • 17

1 Answers1

1

I see a couple of mistakes here.

Your view does not make a distinction between the two cases you need to enhance your check_box_tag to have different names for each check box
eg

check_box_tag('quality', true, false)
check_box_tag('alignment', true, false)

and then in your controller you should handle params differently

if params[:quality] == true
  update_quality_attribute
end
if params[:alingment] == true
  update_alignment_attribute
end

PS: I don't quite get what you do with update_all. If you try to update all records then you should omit the last hash. If you want to update only one record you should

  • use update_attribute or update_column
  • change your route to an on: :member route
xlembouras
  • 8,215
  • 4
  • 33
  • 42
  • Thanks, and just as reference I wanted to update them all at once. That's why I used collection. I was taking a page out of this screencast. http://railscasts.com/episodes/165-edit-multiple-revised I"m currently get a 'missing template evidences/score' which is strange. – gary1410 Mar 02 '14 at 11:24
  • xlembouras, what exactly am I passing in controllers in this line ` `EvidenceScore.update_all({quality: false, alignment: false}, {id: params[:evidence_score_ids]} )` to either `update_quality_attribute` or `update_alignment_attribute` – gary1410 Mar 02 '14 at 23:52