我是RoR的新手,我现在已经陷入这种状况了一段时间。我正在使用一个bootstrap模式,其中一个表显示了数据库中的用户,并且每个都有一个愤怒字段。需要向控制器发送user_id
和范围的值。
像这样:
{0 {user_id1 : range_value1}, 1 {user_id2 : range_value2}}
部分模式:
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="modalLabel">Distribuir Estudos</h3>
</div>
<div class="modal-body">
<table class="table table-bordered">
<thead>
<tr>
<th>Integrante</th>
<th>Papel</th>
<th>Distribuição</th>
</tr>
</thead>
<tbody>
<% @users_protocols.each do |user_protocol| %>
<tr>
<td hidden><%= hidden_field :user_id, value: user_protocol.id %></td>
<td><%= user_protocol.user.username %></td>
<td><%= user_protocol.role.name %></td>
<td>
<span class="range-content">
<input type="range" data-slider-min="0" data-slider-max="100" data-slider-step="1" data-slider-value="100"/>
<span>100</span>
</span>
<span>%</span>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Fechar</button>
<%= link_to distribute_protocol_path, method: :put, :remote => true, class: 'btn btn-primary', id: 'btnDistribute' do %>
Distribuir
<% end %>
</div>
的application.js
$('#btnDistribute').bind('click', function(e) {
e.preventDefault();
#Recover data from modal and send to controller
});
控制器:
def distribute_studies
@users_protocols = Protocol.find(params[:id]).users_protocols.includes(:user).each do |user_protocol|
puts user_protocol.id
puts user_protocol.role.name
puts user_protocol.user.username
end
respond_to do |format|
format.html { render :back }
format.json { head :no_content }
format.js { render :layout => false }
end
end
def distribute
#DO SOMETHING WITH DATA HERE
redirect_to :back
end