Why only save the last record in the table?

Asked

Viewed 89 times

1

Guys, a little help here, please!

<%= form_tag(portabilizar_portabilidades_path, name: 'form', method: :get) do %>
      <table class="table table-condensed table-bordered table-hover">
        <thead>
          <tr>
            <th>&nbsp;</th>
            <th>Nº do contrato</th>
            <th>Nova parcela</th>
            <th>Verba proponente</th>
            <th>Ação</th>
          </tr>
        </thead>
        <tbody>
          <% @autorizacoes.each do |autorizacao| %>
            <tr>
              <td><%= radio_button_tag "autorizacao", autorizacao.id %></td>
              <td><%= autorizacao.numero_contrato %></td>
              <td>
                <input class="string required form-control" placeholder="Digite um valor" type="text" name="nova_parcela" id="portabilidade_nova_parcela">
              </td>
              <td>
                <%= collection_select( :portabilidade, :verba, Verba.all, :id, :descricao, {prompt: "Selecione a verba"}, {class: "form-control"} ) %>
              </td>
              <td><%= submit_tag "Portabilizar", :class => 'btn btn-primary btn-portabilizar', :disabled => true %></td>
            </tr>
          <% end %>
        </tbody>
      </table>
    <% end %>

This is my view and my controller is this:

  def portabilizar

    @autorizacao = Autorizacao.find(params[:autorizacao])
    autorizacao_selecionada = params["autorizacao"].to_i
    @nova_parcela = params[:nova_parcela].to_i
    @verba_proponente = params[:portabilidade][:verba]

I just need that by clicking on my radiobutton, enter the value in the input, click the button and save the values. But this is only happening with the last record. The previous records are being saved as null, how to resolve this? I don’t know what I’m doing wrong.

2 answers

1


I think it would be better if you did it this way:

  <table class="table table-condensed table-bordered table-hover">
    <thead>
      <tr>
        <th>&nbsp;</th>
        <th>Nova parcela</th>
        <th>Ação</th>
      </tr>
    </thead>
    <tbody>
      <% @autorizacoes.each do |autorizacao| %>
        <%= form_tag(portabilizar_portabilidades_path, method: :get) do |f| %>
          <tr>
            <td><%= radio_button_tag "autorizacao", autorizacao.id %></td>
            <td><%= autorizacao.numero_contrato %></td>
            <td>
              <%= text_field_tag "nova_parcela", '', class: "string required form-control", placeholder: "Digite um valor" %>
            </td>
            <td>
              <%= collection_select( :portabilidade, :verba, Verba.all, :id, :descricao, {prompt: "Selecione a verba"}, {class: "form-control"} ) %>
            </td>
            <td><%= submit_tag "Portabilizar", :class => 'btn btn-primary btn-portabilizar', :disabled => true %></td>
          </tr>
        <% end %>
      <% end %>
    </tbody>
  </table>
  • I did that once. This generates a form for each authorization and the problem is that I will be able to mark several radiobuttons and can not be so... The solution everyone talks about is by a different name, type <input type="seu_type" name="model[N][attribute]">. However, I don’t know how to implement this in any way there in my controller...

0

Do it then:

<table class="table table-condensed table-bordered table-hover">
<thead>
  <tr>
    <th>&nbsp;</th>
    <th>Nova parcela</th>
    <th>Ação</th>
  </tr>
</thead>
<tbody>
  <% @autorizacoes.each do |autorizacao| %>
        <%= form_tag(portabilizar_portabilidades_path, method: :get) do |f| %>
    <tr>
      <td><%= radio_button_tag "radio_autorizacao", autorizacao.id %></td>
      <td><%= autorizacao.numero_contrato %></td>
      <td>
        <%= text_field_tag "nova_parcela", '', class: "string required form-control", placeholder: "Digite um valor" %>
      </td>
      <td>
        <%= collection_select( :portabilidade, :verba, Verba.all, :id, :descricao, {prompt: "Selecione a verba"}, {class: "form-control"} ) %>
      </td>
      <td>
        <%= hidden_field_tag "autorizacao", autorizacao.id %>
        <%= submit_tag "Portabilizar", :class => 'btn btn-primary btn-portabilizar', :disabled => true %>
      </td>
    </tr>
  <% end %>
</tbody>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.