Pass only checkboxes checked for other view

95 Views Asked by At

I have two views, the first is a table that show all authorizations, how I make for pass only the authorizations checked for other view?

This is first view: (all ok here)

(...)
 <% @authorizations.each do |authorization| %>
      <tr>
        <td><%= check_box_tag 'authorization_marked' %></td>
(...)
<%= f.button :submit, "To Reserve" %>

My first and second controller:

def index
    if params[:search_employee_by_cpf].present?
      @employees = Employee.search_cpf(params[:search_employee_by_cpf]).all
      @authorizations = Authorization.search_employee_by_cpf(params[:search_employee_by_cpf]).all
    else
      @authorizations = []
    end
  end

  # GET /refinancings/new
  def new
    @employee = Employee.search_cpf(params[:search_employee_by_cpf])
    @authorizations.authorization_marked = true # PROBLEM HERE
    @refinancing = Refinancing.new
  end

In other view I want show just the checked:

  <h3>Reserved value</h3>
  <table class="table table-condensed table-bordered table-striped">
    <thead>
      <th>Contract number</th>
      <th>Parcel value X Quantity of Parcels</th>
    </thead>
    <% @authorizations.each do |authorization| %>
      <tbody>
        <tr>
          <td><%= authorization.contract_number %></td>
          <td><%= number_to_currency authorization.parcel_value %> x <%= authorization.qtd_parcel %></td>
        </tr>
      </tbody>
    <% end %>
  </table>
2

There are 2 best solutions below

0
Elton Santos On BEST ANSWER

I got it! It is My solution, created a repository for this, take a look on code:

https://github.com/eltonsantos/playing_checkboxes

2
tkz79 On

So your first form needs to capture the id numbers of all the rows you want to pass to the second view. In your second action, you need to capture those parameters, and make a collection of the objects you want the second view to load.

Take a very close look at what data is being passed when you post the first form, that's the data you have to use to create the next collection you want.