Rails:提交按钮不提交值

时间:2016-03-07 14:36:37

标签: html ruby-on-rails ruby ruby-on-rails-4

Submit_tag 未提交checkbox_tag的选定ID。

这是代码:

<%= 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.1ms)
  选择“协调员”。* FROM“协调员”WHERE“协调员”。“id”=? ORDER BY“coordinators”。“id”ASC LIMIT 1 [[“id”,1]]   SQL(0.2ms)
  更新“出勤”SET“status”=“出现”,“回合”= 1 WHERE“出席”。“id”IS NULL   考勤负荷(0.1ms)SELECT“考勤”。* FROM“考勤”WHERE“考勤”。“id”IS NULL   重定向到http://localhost:3000/attendances   完成302发现在5ms(ActiveRecord:0.4ms)

单击“提交”按钮后,未通过所选的复选框值。 当我从显示页面返回索引页面时,值不会更新。但刷新之后,它始终如一地运作

3 个答案:

答案 0 :(得分:0)

我认为问题是check_box_tag在未选中时不会发送任何值,因此除非您检查出勤组中至少有一个复选框,否则{{1}控制器中的参数。

为此,如果未选中复选框,则需要将a_ids参数重置为空值。即在你的控制器中添加这样的东西:

a_ids

另请参阅此SO question中的“小问题”部分。

答案 1 :(得分: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

你会注意到我们正在使用一个表来进行“回合” - 如果你正在建立一个像会议这样的域,你肯定希望一个表来存储每个子事件(类,谈话等)。您不希望在注意表中使用弱隐式链接!

通过这种方式创建一个圆形表单,让您设置哪个可以让您设置哪些团队参与是轻而易举的事情:

<%= 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一起使用。然而,这是一个非常高级的主题和整个教程本身,所以我将由你来阅读一些教程并弄清楚。

答案 2 :(得分:0)

当涡轮链接正在缓存页面时,我通过将JS移到onReady来解决了这个问题。