更新表列中的单个单元格

时间:2013-06-12 13:40:52

标签: ruby-on-rails

嘿伙计们我在rails web app上写了我的第一个ruby,它有一个简单的表单,用户可以为他们想要保留的小组自习室输入一些信息,当他们提交时,它会保存到SQLite DB。从那里我有单独的视图,以表格形式显示信息(这是指定房间的管理员将看到的)。

我要做的是在表中添加不属于数据库的其他行。一个有一个下拉框,管理员可以在其中选择房间,第二行是更新按钮,因此当管理员点击它时,它将仅更新具有所选房间号的列,另外第三行显示该单个列的所选房间。我试过用不同的东西来玩,但是无法让它起作用。

以下是我的表格:

http://i.imgur.com/m7udUVP.png

这是表页面的控制器,也是索引页面:

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

以下是表/索引页面的视图:

<h1>Listing students</h1>

<table class ="Tables">
<tr>
<th>Id</th>
<th>N-Number</th>
<th>First Name</th>
<th>Last Name</th>
<th>Date Submit</th>
<th>Floor Preference</th>
<th>Room</th>
<th></th>
<th></th>
<th></th>
<th>Update</th>
</tr>

<% @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>
<td><%= link_to 'Show', :controller => 'students', :action => 'preview', :id => student%></td>
<td><%= link_to 'Destroy', student, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<% 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>
</tr>
<% end %>
</table>
<br />

<%= link_to 'New Student', new_student_path %>

如果有人对谁解决这个问题有任何建议,那将会有很大的帮助!

由于

0 个答案:

没有答案
相关问题