2016-03-07 163 views
0

Submit_tag不提交選中的checkbox_tag的id。Rails:提交按鈕不提交值

這裏是代碼:

<%= form_tag pattendance_attendances_path, method: :put do %> 
    <% if coordinator_signed_in? || admin_signed_in? %> 
    <ul class="list-group"> 
    <li class="list-group-item"> 
    <%= radio_button_tag "round", 1 , true %> Round 1</li> 
    <li class="list-group-item"> 
    <%= radio_button_tag "round", 2 %> Round 2 </li> 
    <li class="list-group-item"> 
    <%= radio_button_tag "round", 3 %> Round 3 </li> 
    <li class="list-group-item"> 
    <%= radio_button_tag "round", 4 %> Round 4 </li> 
    <li class="list-group-item"> 
    <%= radio_button_tag "round", 5 %> Round 5 </li> 
    </li>  
    <li class="list-group-item"> 
     <%= radio_button_tag "mark_present", "present" , true %> Present <br> 
     <%= radio_button_tag "mark_present", "absent" %>Absent 
    </li><br> 
    <li class="list-group-item"> 
    <%= submit_tag "Mark Attendance"%></li> 
    </ul> 
    <% end %> 
    </div> 
<div class="table-responsive"> 
<div class="col-md-8"> 
<table class="table table-hover"> 
<thead> 
<tr> 
    <th>Mark</th> 
    <th><%= model_class.human_attribute_name(:team_id) %> ID</th> 
    <th><%= model_class.human_attribute_name(:event_id) %> Name</th> 
    <th><%= model_class.human_attribute_name(:round) %></th> 
    <th><%= model_class.human_attribute_name(:status) %></th> 
    <th><%=t '.actions', :default => t("helpers.actions") %></th> 
</tr> 
</thead> 
<tbody> 
<% @attendances.each do |attendance| %> 
    <tr> 
    <td><%= check_box_tag "a_ids[]", attendance.id %></td> 
    <td><%= attendance.team_id %></td> 
    <td><%= attendance.event_id %></td> 
    <td><%= attendance.round %></td> 
    <td><%= attendance.status %></td> 
    <td> 
     <%if admin_signed_in? %> 
     <%= link_to t('.edit', :default => t("helpers.links.edit")), 
        edit_attendance_path(attendance), :class => 'btn btn-default btn-xs' %> 
     <%= link_to t('.destroy', :default => t("helpers.links.destroy")), 
        attendance_path(attendance), 
        :method => :delete, 
        :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) }, 
        :class => 'btn btn-xs btn-danger' %> 

     <%end%> 
     <%= link_to t('.show', :default => t("helpers.links.View details")), 
        attendance, :class => 'btn btn-default btn-xs' %> 
     </td> 
     </tr> 
    <% end %> 
    </tbody> 
    </table> 
    </div> 
    </div> 
    <% end %> 

控制器代碼:

def pattendance 
params[:a_ids] 
if params[:mark_present]=="present" 
    Attendance.where(id: params[:a_ids]).update_all(status: 'Present', round: params[:round]) 
else 
    Attendance.where(id: params[:a_ids]).update_all(status: 'Absent', round: params[:round]) 
end  
redirect_to attendances_url 
end 

背後實施此方法的動機是爲了紀念所有在特定事件註冊的球隊參加。 複選框用於選擇多個團隊。 我選擇「Attendance.ids」,因爲一個團隊可以參與多個活動。所以選擇Team IDS可以在所有事件中標記出隊。 爲了讓每個使用參加ID的活動都具有獨特性。

選擇從單選按鈕狀態(存在或不存在),並點擊標記出勤 submit_tag應該更新選定複選框的值之後。 但是 有時值被傳遞,有時它們不是。

請告訴我這段代碼有什麼問題。這將是非常有幫助的。

控制檯: 我嘗試更新循環起選擇複選框爲 「2」 和狀態 「存在」

Parameters: {"utf8"=>"✓", "authenticity_token"=>"2uzivV42tLjwuUd+QyEHvL6tCa8ZCE8xRbgJ5k7ueEcV9oaQt5yLafKmZTiFceZEY9B3wyY52qxL1f0hvqQ47A==", "round"=>"2", "mark_present"=>"present", "commit"=>"Mark Attendance"} 

協調員負荷(0.1毫秒)
SELECT 「協調員」 * FROM。 「協調員」WHERE「協調員」。「id」=? ORDER BY「coordinators」。「id」ASC LIMIT 1 [[「id」,1]] SQL(0.2ms)
UPDATE「attendances」SET「status」='Present','round'= 1 WHERE'attendance 「。」id「IS NULL 考勤負載(0.1ms)SELECT」attendances「。* FROM」attendances「WHERE」attendances「。」id「IS NULL 重定向到http://localhost:3000/attendances 已完成302在5ms中找到(ActiveRecord:0.4ms)

點擊提交按鈕後沒有通過選定的複選框值。 當我從顯示頁面返回到索引頁面時,值不會更新。但刷新後,它始終如一地工作

+0

您不能在Ruby上下文中使用'checked =「checked」'。你所做的只是創建一個名爲'checked'的局部變量,其值爲''checked''。 – meagar

+0

謝謝指出。但是這可以解決我的問題? –

+0

實際上它是一個屬性,它可以在窗體加載時預先選擇單選按鈕,現在所有的單選按鈕都不是預選的。 –

回答

0

我認爲問題是 s不會發送任何值時,因此,除非您至少檢查考勤組中的任何複選框,您不會得到任何控制器中的參數a_ids

爲此,如果未選中複選框,則需要將a_ids參數重置爲空白值。即添加這樣的事情在你的控制器:

def pattendance 
    params[:a_ids] ||= [] 
    ... 
end 

也請參閱本SO question「小疑難雜症」一節。

+0

感謝您的回覆 您提供的鏈接執行一個不同的任務,在這個應用程序中,用戶希望通過從它們中移除檢查來移除類別(已經檢查過) 這裏我的情況完全不同了,儘管我試圖實現這個代碼,但它根本沒有任何幫助。 :( –

+0

鏈接應該進一步解釋了在使用多個check_box_tag與數組參數時Rails的行爲 - 當沒有選中check_box時,控制器中的參數完全沒有定義,我可以看到你的代碼沒有錯,我甚至採取了代碼並進行了測試,行爲是一致的 - 我得到了只有在沒有選中複選框的情況下才會描述錯誤。那麼你是否聲稱你得到了'a_ids'參數零,即使你已經勾選了一個複選框?如果是這樣,我想這個錯誤必須來自其他地方。 – BoraMa

+0

感謝您測試我的代碼。 如果您遇到了一致的行爲,那麼我的問題可能是什麼? -_- 是的,我現在也在想,錯誤可能來自其他地方,但它怎麼會是? –

0

我會給你一點創意 - 但有一種更簡單的方法來完成一次創建/更新多個關聯。

首先確保您擁有建模下來:

class Event < ActiveRecord::Base 
    has_many :registrations, dependent: :destroy 
    has_many :teams, through: :registrations 
    has_many :rounds, dependent: :destroy 
    has_many :attendences, through: :rounds 
    validates_uniqueness_of :name 
end 

class Team < ActiveRecord::Base 
    has_many :registrations, dependent: :destroy 
    has_many :events, through: :registrations 
    has_many :attendences, dependent: :destroy 
    has_many :rounds, through: :attendences 

    validates_uniqueness_of :name 
end 

# Many To Many Join model between Event and Team 
class Registration < ActiveRecord::Base 
    belongs_to :event 
    belongs_to :team 
end 

class Round < ActiveRecord::Base 
    belongs_to :event 
    has_many :attendences, dependent: :destroy 
    has_many :attending_teams, through: :attendences, source: :team 
end 

class Attendence < ActiveRecord::Base 
    belongs_to :team 
    belongs_to :round 
    has_one :event, through: :round 
end 

這裏,我們使用的是表「回合」你會發現 - 如果你正在建模一個域,如會議,你definatly希望表來存儲每個子事件(班級,談話等)。您不希望在attendences表中使用弱隱式鏈接!

武裝與此創建一個圓,它可以讓你設置它可以讓你設置哪支隊伍參加形式是輕而易舉的事:

<%= form_for(@round) |f| %> 
    <div class="field"> 
    <% f.label(:attending_teams_ids) %> 
    <% f.collection_check_boxes(:attending_teams_ids, @round.event.teams, :id, :name) %> 
    </div> 
    <% f.submit %> 
<% end %> 

Rails有很多有用的工具像collection_check_boxes從關聯創建投入。

如果您需要更新一次,你會使用accepts_nested_attributesfields_for在一起的多輪。然而,這是一個相當先進的主題和整個教程本身,所以我會留給你看幾個教程,並弄清楚。

+0

非常感謝您爲我設計它,我真的很感激它。 :) 我會實現這個代碼,但我必須使上面的代碼工作。我無法弄清楚問題所在。 –