I have a table in a database with two column开发者_运维问答s, Name and ID. I have a rails model for it named FakeTable.
I have a controller for the model:
class FakeTablesController < ApplicationController
def index
@data = FakeTable.find(:all)
end
def to_index
redirect_to( :action => "index" )
end
def update
redirect_to( :action => "index" )
end
end
I have a form to display the information in this table:
<table>
<thead>
<tr>
<th><%= "Name" %></th>
</tr>
</thead>
<tbody>
<% form_tag :action => "update" %>
<% @data.each_with_index do |d, index| %>
<% fields_for "d[#{index}]", d do |f| %>
<tr>
<td class="tn">Name: <%= f.text_field :name %></td>
<td class="tn">Id: <%= f.text_field :id %></td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
<%= submit_tag 'Update' %>
<%= end_form_tag %>
I have two questions. First, how do I use the ID to access each row in the table to display the data? Currently I use these two lines of code to access get each row in the table:
<% @data.each_with_index do |d, index| %>
<% fields_for "d[#{index}]", d do |f| %>
I would like to change this so it uses the ID column instead.
My second question is, when I click the "Update" button, I want the data in the table to be updated from text fields in the form. How would I do that? I am using ruby on rails 2.3.8. Thanks!
精彩评论