0
votes

I'm trying to use radio buttons to make administrators approve users. So in the page I have a list for all the users waiting for approval (a true/false value in user model). After the administrator has set "yes" or has left "no" on the radio button field of all of them, he should press the unique submit button.

I'm trying to figure out how to pass to the controller all of the users for the approval update.`

<%= 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 %>

On submit I should get "approved[id]"=>{"true/false"} right? I'm very new on all of this. Thank you

1
Look at the params hash in your server window, you know, where you started rails server or ruby script/server. The top of each output shows you the params. It will answer a lot of your questions. Post that here if you have problems understanding/ manipulating that.Anil

1 Answers

0
votes

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?