Create New method in Rails

1
How do I create a method in Rails that queries the database in a given table and shows in View , and how do I call this method and where do I create it?

def somar    
  @value = Registro.sum('saldo')
end

How do I call this method in view and put it in a label ?

    
asked by anonymous 14.07.2016 / 16:34

2 answers

1

Well, I'm going to consider that you already have Rails properly installed and configured on your machine.

It's unclear if your method is really a Model method or a action method (a Controller method), but I think it's a action you are calling a method that is probably a method of a Template . Well considering these right assumptions we can take the following steps to make this work.

We create our Controller and our action (we will use generator to speed up the procedure)

  

rails g controller operations add

That will generate our controller in app/controllers/operacoes_controler.rb

class OperacoesController < ApplicationController
  def somar
  end
end

And it will generate our view in app/views/operacoes/somar.html.erb

<h1>Operacoes#somar</h1>
<p>Find me in app/views/operacoes/somar.html.erb</p>

Ok now we're almost there, I do not know your model Registro , nor what you want to add, I'll consider that it has a saldo field and you want to add the value of every saldo strong> records .

*** For testing I generated model Registro , but you probably will not need to perform this step, I'll put it here to demonstrate how I performed the procedure.

  

rails g model log balance: integer

*** In the console I created 3 registers to test the logic, which you probably will not have to do either.

2.3.1 :001 > Registro.create(saldo: 1)
 => #<Registro id: 1, saldo: 1, created_at: "2016-07-14 23:56:46", updated_at: "2016-07-14 23:56:46"> 

2.3.1 :002 > Registro.create(saldo: 2)
 => #<Registro id: 2, saldo: 2, created_at: "2016-07-14 23:56:50", updated_at: "2016-07-14 23:56:50"> 

2.3.1 :003 > Registro.create(saldo: 3)
 => #<Registro id: 3, saldo: 3, created_at: "2016-07-14 23:56:53", updated_at: "2016-07-14 23:56:53"> 

Question answer

Place the value assignment in action of OperacoesController

class OperacoesController < ApplicationController
  def somar
    @valor = Registro.sum('saldo')
  end
end

And put @valor between tags erb <%= @valor =%> in your View ( app/views/operacoes/somar.html.erb ). Example:

<h1>A soma dos saldos de todos os registros é <%= @valor %>  </h1>

Access: http://localhost:3000/operacoes/somar

And the result will look something like this:

EDIT: If you wanted to reuse this method in several Views you can use a Helper as Rafael quoted. That is, remove from OperacoesController and add in ApplicationHelper , ex:

# /app/helpers/application_helper.rb
module ApplicationHelper
  def saldo_total
    Registro.sum('saldo')
  end
end

In your View it would look like this

#app/views/operacoes/index.html.erb
 <h1>A soma dos saldos de todos os registros é <%= saldo_total %>  </h1>
    
15.07.2016 / 02:07
0

In this case you will need to define your method in a helper. Helpers are classes with auxiliary methods available in views and also in controllers (via #helpers), as shown in table 1.

module ApplicationHelper
    def identity(user)
        return "#{user.name} - #{user.email}"
    end
end

Table 1. Code with an example helper method

No one is going to explain better than the helpers session in the official Ruby on Rails documentation, follow the link , I recommend a full reading (even if English, reading is easy).

    
15.07.2016 / 16:21