0

我正在尝试使用单选按钮让管理员批准用户。因此,在页面中,我列出了所有等待批准的用户(用户模型中的真/假值)。管理员在所有的单选按钮字段中都设置了“是”或“否”后,他应该按下唯一的提交按钮。

我试图弄清楚如何将所有用户传递给控制器​​以进行批准更新。`

<%= form_tag(approve_url, :html => { class: "form-inline"}  )do |f| %> 
  <table>
    <% @tobeapproved.each do |user| %>
      <tr >
        <td>
          <%= user.name %>
        </td>
        <td>
          <%= user.surname %>
        </td>
        <td>
          <%= user.email %>
        </td>
        <td>
          <%= label :approved %>
          <%= radio_button_tag "approved[#{user.id}]", "true" %> Approved
          <%= radio_button_tag "approved[#{user.id}]", "false" %> Not approved
        </td>
      </tr>
    <% end %>
  </table>
  <%= f.submit %>
<% end %>

提交时我应该得到“approved[id]”=>{“true/false”} 对吗?我对这一切都很陌生。谢谢

4

1 回答 1

0

In the end I figured out how to do such a thing.

<%= form_tag(approve_url, :html => { class: "form-inline"}) do |f| %> 
  <table>
    <% @tobeapproved.each do |user| %>
    <tr >

      <td>
        <%= user.name %>
      </td>
      <td>
        <%= user.surname %>
      </td>
      <td>
        <%= user.email %>
      </td>
      <td>
        <%= radio_button_tag "approved[#{user.id}]", "true"  %> Approved
        <%= radio_button_tag "approved[#{user.id}]", "false", true  %> Not approved
      </td>
    </tr>
  <% end %>
</table>
<%= submit_tag 'Confirm'%>

It gets to me something like this in params:

"approved"=>{"27"=>"false", "26"=>"false", "25"=>"true"}

And in my controller:

 def update
   @users = params[:approved]
   logger.debug "The object is #{@users}"

   respond_to do |format|
   @users.each_pair do |key, value|
     user = User.find(key)
     user.update_attribute(:approved, value)
   end
   format.html { redirect_to :action => :list, notice: 'Users updated' }
end

But I imagine this code kind of sucks. Anybody knows how to better it?

于 2012-06-07T15:42:32.713 回答