Counter in model

0

I'm looking for a way to add a variable with increment to add to the id of a div, could anyone tell me the most recommended way to do this?

My difficulty in making this increment is due to cocoon that is used to call this render

<%= link_to_add_association '+', f, :filtro_questoes,
        'data-association-insertion-node' => "#filter_q",
        'data-association-insertion-method' => "append", id: 'add', class: 'btn btn-primary' %>

already tried to access object attribute , @count , but nothing worked out

obs: If you have a better way of doing this without calling the model attribute, it's also worth = D

I need to improve my ruby knowledge, but I have to finish what I'm doing right away.

model

class Carro < ActiveRecord::Base
    attr_accessor :count

    belongs_to :person

    def self.count(n = 0)
        @count = n+1
    end

    def self.count
        @count
    end

end

view

<div class="control-group nested-fields">

    <div id="carro_div_n <%= f.count %>" class="row form-group" style="border: 1px solid blue; padding: 10px; margin: 10px;">
      <div class="field">
        <h3>Descrição...</h3>
        <%= f.label :carro_id %>
      </div>

      <div class="field col-xs-3">
        <%= f.label :marca_id %>
        <%= f.select :marca_id, 
        options_for_select(
          @marcas.collect { |marca|
          [marca.name.titleize, marca.id] }, 0), {prompt: 'Selecionar marca'}, { id: 'marcas_select', class: 'form-control'} %>
      </div>
      <div id="div_modelo_n" class="field col-xs-3">
        <%= f.label :modelo_id %>
      <%= f.select :modelo_id, options_for_select(@modelos.collect { |modelo|
          [modelo.name.titleize, modelo.id] }, 0), {}, { id: 'modelos_select', class: 'form-control' } %>
      </div>
      <div class="checkbox form-group col-xs-2">
        <%= link_to_remove_association "remove tarefa", f, class: 'btn btn-danger' %>
      </div>
    </div>
</div>
    
asked by anonymous 09.12.2016 / 14:27

2 answers

1

First, when you insert self into a method it turns a static method, static into Java , ie you should call it like this: Carro.count .

If you want an id, simply use the id of the car: Carro.id

But if you really want to make a count, do so when it comes to showing all cars. For example, if you are using each to show all cars:

<% Carro.all.each_with_index do |carro, count| %>
    <div id="<%= carro.count %>">
        ...
    </div>
<% end %>
    
09.12.2016 / 18:56
1

Have you tried instead of <%= f.count %> use <%= @carro.count %> ?

    
09.12.2016 / 20:48