Passing data from model to views

Hello everyone,

I have a situation where I need to send processed data from model to
view.

Current working mechanism is,
View (_form.html.erb)

I have a link
<%= link_to “receipt summary”, receipt_rpts_create_path(from_date:
‘fdate’,
to_date: ‘tdate’), :class => “btn btn-warning”, method: “get” %>

and some other text fields like share_amt, loan_balance etc, which
needs
to be updated later based on processed data received from the model.

which will send from date and to date to the controller (which is I’m
not
getting in correct format but I’ve checked whether parameters are
received
in controller by coding puts"#{fdate}" and its showing the output in
stdout as fdate).

Both dates are received using “params[:from_date]” and "params[:to_date]
and sent to model through controller as
Controller (reciept_rpts_controller.rb)

ReceiptRpt.receiptsummary(fdate,tdate)

In the model I’ve self.receiptsummary(from_date, to_date) method, where
I
calculate share_amt, loan_balance, etc.

Now after calculating all those required values, I need to send
share_amt
and loan_balance etc back to view and update the view’s text field with
these calculated values.

In basic rails app all these values which are processed in values are
stored to database and then view (eg: index.html.erb) will fetch data
from
database and present to view. But in my situation I don’t need to store
values to database. I want to show them on web page at that moment.
How can I achieve this functionality?

Thank you.

Personally, I would view the Receipt Summary as its own class, maybe
ReceiptRpt::ReceiptSummary. I’d probably do the work currently in your
receiptsummary method in the initialize method, then store the values
that
you need in your view in instance variables, and define attr_readers for
them. Example:

class ReceiptRpt::ReceiptSummary
attr_reader :share_amt, :loan_balance

def initialize(fdate, tdate)

… do work and assign @share_amt, @loan_balance,

and any other data you need access to later on

end
end

Usage:

@summary = ReceiptRpt::ReceiptSummary.new(Date.new(2016, 5, 1),
Date.new(
2016, 5, 31))

@summary.share_amt #=> Share Amount
@summary.loan_balance #=> Loan Balance

Hi Padmahas,

Based on my understanding, you can return the processed values from
model
to controller then to view like below sample definition.

/in controller
def create_reciept_summary
@result = ReceiptRpt.receiptsummary(fdate,tdate)
end

/in model

def self.receiptsummary(fdate,tdate)
----calculation—
return share_amt.to_s + “:” + loan_balance.to_s
end

/in view
<% share_amt = @result.split(“:”)[0] %>
<% loan_balance = @result.split(“:”)[1] %>

Hope it will help.

Regards,
Seeni Rafiyullah Khan A,
[email protected]In Every moment, thank God.

P Please consider the environment before printing this email