【问题标题】:Rails : Submit button not submitting valuesRails:提交按钮不提交值
【发布时间】:2016-03-07 14:36:37
【问题描述】:

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。

从单选按钮和 status(出席或缺席)中选择 rounds 并点击 ma​​rk出席 submit_tag 后,它应该会更新选中的复选框。 但 有时会传递值,有时则不会。

请告诉我这段代码有什么问题。这真的很有帮助。

控制台: 我尝试将选中的复选框更新为“2”,状态为“存在”

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

协调器负载 (0.1ms)
SELECT "coordinators".* FROM "coordinators" WHERE "coordinators"."id" = ? ORDER BY "coordinators"."id" ASC LIMIT 1 [["id", 1]] SQL (0.2ms)
更新“出席”设置“状态”=“出席”,“回合”= 1,其中“出席”。“id”为空 出勤负载 (0.1ms) SELECT "attendances".* FROM "attendances" WHERE "attendances"."id" IS NULL 重定向到http://localhost:3000/attendances Completed 302 Found in 5ms (ActiveRecord: 0.4ms)

点击提交按钮后没有传递选中的复选框值。 当我从显示页面返回到索引页面时,这些值不会更新。但刷新后它始终有效

【问题讨论】:

  • 您不能在 Ruby 上下文中使用 checked="checked"。您所做的只是创建一个名为checked 的局部变量,其值为"checked"
  • 感谢指出。但这可以解决我的问题吗?
  • 实际上它是一个在表单加载时预选单选按钮的属性,现在所有单选按钮都没有被预选。
  • 不,你错了。这不是 Ruby 的工作方式。 radio_button_tag 的第三个参数是一个 boolean,它指示是否检查该字段。您使用的是checked="checked"纯属巧合。默认检查无线电标签的正确方法是简单地使用radio_button_tag "round", 1, true
  • 谢谢我已经更改了我的代码,但仍然有一些问题:|提交按钮每次都无法正常工作:(

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


【解决方案1】:

我认为问题在于check_box_tags 在未选中时不会发送任何值,因此除非您在出勤组中至少选中一个复选框,否则您将不会在a_ids 参数中获得任何值控制器。

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

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

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

【讨论】:

  • 感谢您的回复。您提供的链接执行不同的任务。在该应用程序中,用户希望通过从中删除检查来删除类别(已检查)。在这里,我的情况完全不同。虽然我试图实现这段代码,但它根本没有帮助。 :(
  • 该链接应该进一步解释了在使用多个 check_box_tags 和数组参数时 Rails 的行为 - 当没有选中复选框时,控制器中的参数完全未定义。我看不出您的代码没有任何问题,我什至拿走了代码并对其进行了测试,并且行为是一致的 - 我只有在选中 NO 复选框时才收到您描述的错误。那么,即使您已选中复选框,您是否声称您获得了 a_ids 参数 nil ?如果是这样,我认为错误一定来自其他地方。
  • 感谢您测试我的代码。如果您遇到一致的行为,那么我的问题可能是什么? -_- 是的,我现在也在想,错误可能来自其他地方,但怎么可能呢?
  • 当我从显示页面返回到索引页面时,这些值不会更新。但刷新后它始终有效。
  • 对不起,你能澄清最后的评论吗?您是否在使用后退浏览器按钮(在这种情况下,难怪您会看到历史数据,而不是更新数据)?此外,显示和索引操作与您在 Q 中发布的编辑表单有什么共同点?我不得不说,我很困惑。
【解决方案2】:

我会给你一些创造性的分数 - 但有一种更简单的方法可以同时完成创建/更新多个关联。

首先确保您已完成建模:

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。然而,这是一个非常高级的主题和整个教程本身,所以我会留给你阅读一些教程并弄清楚。

【讨论】:

  • 非常感谢为我设计它,我真的很感激。 :) 我将实现此代码,但我必须使上述代码也能正常工作。我无法找出问题所在。
【解决方案3】:

我通过将我的 JS 移动到 onReady 解决了这个问题,因为 turbolinks 正在缓存页面。

【讨论】:

    猜你喜欢
    • 2011-05-06
    • 2014-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-29
    • 2011-04-07
    • 2015-09-19
    • 1970-01-01
    相关资源
    最近更新 更多