2014-03-02 36 views
0

我試圖更新只更新未選中時設置爲false的屬性的窗體。他們目前默認爲true(這是我想要的),但如果我點擊一個屬性,它會將它們全部更新爲false。我只想更新一個特別是假的,以及許多其他行。我不知道如何進行檢查以說「如果選擇了某個屬性,請更新該屬性,否則如果選擇了其他屬性,則更新另一個屬性,並立即更新所有收集/檢查的字段。」提交複選框表單時只更新某些屬性

我已經在我的路線定義的集合:

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

這是我的控制器:

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 

這是我的形式:

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

當你點擊如果我只選擇其中一個複選框,那麼它們都會更新。如果我選擇一個複選框,我試圖讓只有一個更新的能力。通過點擊複選框並提交它,它應該呈現「否」

下面是發生了什麼的圖片。

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

回答

1

我看到一對夫婦的錯誤在這裏。

您的看法並不能讓你需要提高你的check_box_tag有不同的名稱爲每個複選框

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

,然後在你的控制器,你應該處理PARAMS不同

兩種情況之間的區別
if params[:quality] == true 
    update_quality_attribute 
end 
if params[:alingment] == true 
    update_alignment_attribute 
end 

PS:我不太清楚你用update_all做什麼。如果您嘗試更新所有記錄,那麼您應該省略最後一個散列。如果您想更新紀錄只有一個,你應該

  • 使用update_attributeupdate_column
  • 改變你的路線,一個on: :member航線
+0

謝謝,只是作爲參考,我想一次全部更新。這就是我使用收藏的原因。我正在瀏覽這個截屏視頻。 http://railscasts.com/episodes/165-edit-multiple-revised我現在得到了一個'丟失的模板證據/得分',這很奇怪。 – gary1410

+0

xlembouras,我在這一行傳遞的控制器究竟是什麼''EvidenceScore .update_all({quality:false,alignment:false},{id:params [:evidence_score_ids]})'update_quality_attribute'或'update_alignment_attribute' – gary1410