2016-03-07 164 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

非常感谢您为我设计它,我真的很感激它。 :) 我会实现这个代码,但我必须使上面的代码工作。我无法弄清楚问题所在。 –