Ruby on rails 使用单击按钮更新表字段

Ruby on rails 使用单击按钮更新表字段,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,每次单击该行中的按钮时,我都试图使用该行下拉框中的数据更新表中的单元格。每行都有一个下拉框和一个按钮,如下图所示: 我正在尝试设置它,以便当用户从下拉框中选择一个值并单击“更新”按钮时,它将仅更新该行的房间列的值。但我不知道如何让按钮工作,我想看看是否有人能帮我 这是我的控制器: def index @students = Student.all @first_floor = %w(1101 1102 1103 1104 1105) @second_floor = %w(2101 2102 2

每次单击该行中的按钮时,我都试图使用该行下拉框中的数据更新表中的单元格。每行都有一个下拉框和一个按钮,如下图所示:

我正在尝试设置它,以便当用户从下拉框中选择一个值并单击“更新”按钮时,它将仅更新该行的房间列的值。但我不知道如何让按钮工作,我想看看是否有人能帮我

这是我的控制器:

def index
@students = Student.all
@first_floor = %w(1101 1102 1103 1104 1105)
@second_floor = %w(2101 2102 2103 2104)
@third_floor = %w(3101 3102 3103 3104)

@selected_room = params[:room]

respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @students }
end
end
以下是表的视图部分:

<% @students.each do |student|%>
<tr>
<td><%= student.id %></td>
<td><%= student.n_number %></td>
<td><%= student.f_name %></td>
<td><%= student.l_name %></td>
<td><%= student.date_submit %></td>
<td><%= student.floor_pref %></td>
<td><%= @selected_room %></td>
<% form_tag do %>
<% if student.floor_pref == '1st' %>
    <td><%= select_tag 'room', options_for_select(@first_floor.map { |value| [value,value]}, @selected_room) %></td>
<% end %>
<% if student.floor_pref == '2nd' %>
    <td><%= select_tag 'room', options_for_select(@second_floor.map { |value| [value,value]}, @selected_room) %></td>
<% end %>
<% if student.floor_pref == '3rd' %>
    <td><%= select_tag 'room', options_for_select(@third_floor.map { |value| [value,value]}, @selected_room) %></td>
<% end %>
<td><%= submit_tag 'Update' %></td>
<% end %>
<td><%= button_to 'Show', :controller => 'students', :action => 'preview', :id => student%></td>
<td><%= button_to 'Remove', student, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>

‘学生’,:action=>‘预览’,:id=>student%>

按钮标签在页面上创建表单。您现在遇到的问题是select_标签下拉列表不是该表单的一部分。您可能想要做的是显式地创建表单,并将下拉列表放在表单中。将最后2个td替换为以下内容:

<%= form_tag do %>
  <% if student.floor_pref == '1st' %>
      <td><%= select_tag 'room', options_for_select(@first_floor.map { |value| [value,value]}, @selected_room) %></td>
  <% end %>
  <% if student.floor_pref == '2nd' %>
      <td><%= select_tag 'room', options_for_select(@second_floor.map { |value| [value,value]}, @selected_room) %></td>
  <% end %>
  <% if student.floor_pref == '3rd' %>
      <td><%= select_tag 'room', options_for_select(@third_floor.map { |value| [value,value]}, @selected_room) %></td>
  <% end %>
  <td><%= submit_tag 'Update' %></td>
<% end %>


是否要将房间首选项保存到数据库并更新视图中的表,对吗?否,我不想将“房间”保存到数据库。Id、N号、名字、姓氏、日期和楼层优先是DB的一部分。我只是希望这样,当用户从下拉框中选择一个房间并单击“更新”按钮时,该房间将显示在该列中,直到用户决定删除该列。如果他选择该房间并进行更新,该怎么办。然后他重新访问页面,因为它没有保存在DB中,他将永远找不到他选择的文件室。如果你不打算将它保存在任何地方,你不需要点击服务器用所选的文件室更新html表。只需使用jquery/javascript就可以用所选的值更新列。:)问题是,如果我要将其保存到数据库中,我不需要在两个表之间创建带有外键约束的关系吗?如果是这样的话,我真的不知道如何在ruby中做到这一点,这是我尝试的另一种方式。那么您的下一步将是定制表单,以便您知道它需要影响哪一行。您编写的代码将显示@selected_room,每行都相同。感谢您的回复。当我尝试在页面上查看源代码时,下拉框和更新按钮消失了,那一行中是否有我们期待的表单?如果没有,您可能希望扩展表单,使其包含整个tr元素。对不起。我错过了表单标签行上的“=”嗯,现在当我点击更新按钮时,它会将我带到学生表单,学生在那里填写他们的信息,并显示字段缺少的所有验证约束。。。我只需要将所选房间添加到学生记录中,然后在该行中显示它。