Passing data from model to views

20 views
Skip to first unread message

Padmahas Bn

unread,
Jun 21, 2016, 1:50:03 AM6/21/16
to Ruby on Rails: Talk
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.

Rafi A

unread,
Jun 21, 2016, 2:03:31 AM6/21/16
to rubyonra...@googlegroups.com
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,
In Every moment, thank God.

P Please consider the environment before printing this email


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/a3fef5db-313a-4dd1-8b7f-8ff064185545%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

j...@room118solutions.com

unread,
Jun 21, 2016, 12:31:25 PM6/21/16
to Ruby on Rails: Talk
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
Reply all
Reply to author
Forward
0 new messages