我在Rails工作,允许管理员为用户提供虚构资金。在我看来,有以下步骤:
我有一个有效但不优雅的代码。我想知道接近这种情况的正确“Rails”方式是什么。具体来说,我正在寻求你的帮助,有以下几点:
我最重要的问题是#1和#2。我将不胜感激任何提示/帮助。非常感谢你!
#This the action in the controller corresponds to the first bullet point
def index
@users = User.all
@size = @users.length
end
#This the action in the controller corresponds to the third bullet point
def provide_currency
array = *(1..params[:size])
for i in array
user = User.find(i)
user.money += params[i.to_s].to_f
user.save
end
redirect_to admin_path
end
#This is the index.html.erb that corresponds to the second bullet point
<%= form_tag provide_currency_path do %>
<table class="table table-striped table-condensed">
<thead>
<tr>
<th>User</th>
<th>E-mail</th>
<th>New endowment</th>
</tr>
</thead>
<tbody>
<% for user in @users %>
<tr>
<td><%= user.name%></td>
<td><%= user.email%></td>
<td>
<%= number_field_tag (user.id) %>
</td>
</tr>
<% end %>
</tbody>
</table>
<%= hidden_field_tag "size", @size %>
<%= submit_tag "Provide", :class => "btn" %>
<% end %>