Reputation: 597
I'm trying to update cell in my table with the data from a drop down box in that row each time the button is clicked in that row. Each row has a drop down box and a button as you can see in this image:
https://i.sstatic.net/mcjos.png
I'm trying to set it up so that when user selects a value from a drop down box and clicks the update button it will update value of Room column only for that row. But I can't figure out how to get the button even working and wanted to see if anyone can help me with this.
Here is my controller:
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
Here is the part of the view for the table:
<% @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 %>
Upvotes: 0
Views: 1830
Reputation: 4801
button_tag creates a form on your page. The problem that you have right now is that the select_tag dropdown is not part of that form. What you probably want to do is to create the form explicitly and have the dropdown be inside of it. Replace your last 2 td's with something like this:
<%= 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 %>
Upvotes: 1