undefined method `txnid' for []:Array - ruby-on-rails

I am trying to display the unique trasactions in my table. Actually I have some duplicate transactions as well. I am getting this below error when I call txnid.uniq on my table.(#all_settlement_details)
undefined method `txnid' for []:Array
Transaction.html.erb
<tr>
<th>Merchant Name</th>
<th>Transaction ID</th>
<th>Payment Mode</th>
<th>Amount</th>
<th>Status</th>
<th>Transaction Fee</th>
<th>Discount</th>
<th>Additional Charges</th>
<th>Added On</th>
</tr>
</thead>
<tbody id="details">
<% #all_settlement_details.txnid.uniq.each do |sd| %>
<tr>
<td><%= sd.merchantname %></td>
<td><%= sd.txnid %></td>
<td><%= get_mode_ui.value(sd.mode) %></td>
<td><%= sd.amount %></td>
<td><%= get_status(sd.status) %></td>
<td><%= sd.mer_service_fee %></td>
<td><%= sd.discount %></td>
<td><%= sd.additional_charges%></td>
<td><%= get_added_on_date sd.addedon %></td>
</tr>
<% end %>
</tbody>
traansactions_helper.rb
def get_all_settlement_details
# Offset increments by 10 and Limit remains 11
_txn_filter_scope = {'transaction_status' => ["status", (params[:transaction_status].split(',') rescue nil)],
'payment_options' => ["payment_options", (params[:payment_options].split(',') rescue nil)],
'transaction_id' => ['transaction_id', params[:transaction_id]],
'transaction_amount' => ['amount_range', ((params[:transaction_amount].split(',')[0] || 0) rescue nil), ((params[:transaction_amount].split(',')[1] || 100000000) rescue nil)],
'merchant_mid' => ['merchant_mid',params[:merchant_mid]],
'merchant_name' => ['merchant_name', params[:merchant_name]],
'clicks' => ['txn_offset', ((params[:clicks].to_i)*10 rescue 0)]
}
method_array = [['settlement_details'],["date_filter", #start_date, #end_date]]
method_array << ["txn_limit", 11] unless #csv_format
method_array << ['merchant_mid', #merchant_mid] if #merchant_mid
records = call_scope_methods_dynamically(method_array, _txn_filter_scope)
records.empty? ? nil : records
end
# Get the multiple payment modes of a trasaction, if any.
def get_multiple_payment_modes txn_id, payment_modes
debugger
PG_MODE
#all_modes = []
#total_trasactions = []
_multiple_modes = {'transaction_id' => ['transaction_id', params[:transaction_id]]}
methods = [['settlement_details'],['merchant_mid'],['transaction_id'], ['payment_modes']]
#total_transactions << ['transaction_id', txn_id] if payment_modes.present?
if #total_transactions[:payment_modes] > 1
payment_modes.each do |m|
#all_modes << m
end
#all_modes
end
end
def get_uniq_transactions txn_id
#all_modes = []
uniq_tr = #all_settlement_details.txn_id.uniq
uniq_tr.payment_modes.each do |m|
#all_modess << m
end
end
How to fix this error and display only unique transactions?
Thanks for your help!

From what I'm seeing, my best guess is that you are calling txnid on an Array, obviously, you are going to see this error.
This error might be coming from your view template, where you are calling txnid on #all_settlement_details, which is not available.
What you might need is, #all_settlement_details.uniq.pluck(:txnid) instead of #all_settlement_details.txnid.uniq in your view template.
PS: I would prefer not to have this logic in the helper as it is shared across the view of the app. Instead, I would like to use a decorator(draper/hand rolled plain ruby object) here.

Related

How to iterate each value which retrieve from DB and display them in UI?

How to iterate each value which retrieve from DB and display them in UI?
Below the code I am trying
Controller
def execution_results
executionId1=params['executionId1']
executionId2=params['executionId2']
execution_results1 = ExecutionResult.where(:execution_id => executionId1).pluck(:parametersname,:actual_value)
execution_results2 = ExecutionResult.where(:execution_id => executionId2).pluck(:parametersname,:actual_value)
puts execution_results1
puts execution_results2
end
Now getting below response for execution_results1 and execution_results2 in Controller.
7.0ms) SELECT "execution_results"."parametersname", "execution_results"."actual_value" FROM "execution_results" WHERE "execution_results"."execution_id" = ? [["execution_id", 8]]
(0.1ms) SELECT "execution_results"."parametersname", "execution_results"."actual_value" FROM "execution_results" WHERE "execution_results"."execution_id" = ? [["execution_id", 9]]
Device.usage 12
Device.name A1
Device.number 12345
Device.usage 13
Device.name A1
Device.number 12346
Now requirement is to compare above result and show them in Ui as below.Can anyone please help how to bring like this?
|S.No |Param Name |Value|Param Name |Value|Matched|
| 1. Device.usage 12 Device.usage 13 No
| 2. Device.name A1 Device.name A1 Yes
Assuming execution_results1 and execution_results2 have the same number of records,
You can show the results in above-mentioned format by following lines in view
<table>
<tr>
<th>Sr.no</th>
<th>Param Name</th>
<th>Value</th>
<th>Param Name</th>
<th>Value</th>
<th>Matched</th>
</tr>
<% #execution_results.each do |result, index| %>
<tr>
<td><%= index + 1 %></td>
<td><%= result.name %></td>
<td><%= result.value %></td>
<td><%= #execution_results2[index].name %></td>
<td><%= #execution_results2[index].value %></td>
<td><%= compare_function(result, #execution_results2[index]) %></td>
</tr>
<% end %>
</table>
compare_function here is a helper function which will be in your controller's helper and will look something like below
(execution_results1.usage == execution_results2.usage) && (execution_results1.number == execution_results2.number)
OR
Whatever condition you want to compare
Don't forget to change
execution_results1, execution_results
to
#execution_results1, #execution_results2.to_a
in your controller

Rails - Redirect to specific record page

I'm pretty new to Ruby on Rails and Ruby in general but I'm trying to make a small website with simple database in Ruby on Rails.
At the moment I have the html.erb pages to show, add and edit records.
The next thing i wanted to do is the action that redirects user to a page with more info about the record he clicked in the record table.
I can't really think of any way to do this.
Any help would be really appriciated.
p.s. Sorry for any mistakes in my English - it's not my first language and im still learning!
Here is my html code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="tablecontainer">
<table class="table table-bordered table-condensed">
<tr class="success">
<td><b>Nazwa</b></td>
<td><b>Obrażenia</b></td>
<td><b>Typ</b></td>
<td><b>Waga</b></td>
<td><b>Zasięg</b></td>
<td><b>Szybkość</b></td>
<td><b>Rzadkość</b></td>
<td><b>Opcje</b></td>
</tr>
<% #biala.each do |b| %>
<tr>
<td><%= b.nazwa %></td>
<td><%= b.obrazenia %>%</td>
<td><%= b.typ %></td>
<td><%= b.waga %></td>
<td><%= b.zasieg %></td>
<td><%= b.szybkosc %></td>
<td><%= b.rzadkosc %></td>
<td><%= link_to '', {id: b.id, action: 'db_wiecejbiala'}, class: "glyphicon glyphicon-info-sign" %><%= link_to '', {id: b.id, action: 'db_edytujbiala'}, class: "glyphicon glyphicon-pencil" %> <%= link_to '', {id: b.id, action: 'usunbiala'}, data: {confirm: 'Jesteś tego pewien?'}, class: "glyphicon glyphicon-remove" %></td>
</tr>
<% end %>
</table>
And here is the controller:
class BazaController < ApplicationController
def db_bronbiala
#biala = BronBiala.all
#iloscbiala = BronBiala.count
end
def db_dodajbiala
#nowybiala = BronBiala.new
end
def utworzbiala
#nowybiala = BronBiala.new(parametrybiala)
if #nowybiala.save
redirect_to(action: 'db_bronbiala')
else
render('db_dodajbiala')
end
end
def parametrybiala
params.require(:bron_biala).permit(:nazwa, :obrazenia, :typ, :waga, :zasieg, :szybkosc, :rzadkosc, :zalety, :wady, :ciekawostki, :opis)
end
def usunbiala
usuwaniebiala = BronBiala.find(params[:id]).destroy
#biala = BronBiala.all
render('db_bronbiala')
end
def db_edytujbiala
#biala = BronBiala.all
#edytowanabiala = BronBiala.find(params[:id])
end
def aktualizujbiala
#biala = BronBiala.all
#edytowanabiala = BronBiala.find(params[:id])
if #edytowanabiala.update_attributes(parametrybiala)
redirect_to(action: 'db_bronbiala')
else
render('db_edytujbiala')
end
end
def db_wiecejbiala
#biala = BronBiala.all
#bialawiecej = BronBiala.find(params[:id])
end
end
And the db_bialawiecej code:
<div class="content">
<h2>Lista:</h2>
<div class="tablecontainer">
<table class="table table-bordered table-condensed">
<tr class="success">
<td><b>Nazwa</b></td>
<td><b>Obrażenia</b></td>
<td><b>Typ</b></td>
<td><b>Waga</b></td>
<td><b>Zasięg</b></td>
<td><b>Szybkość</b></td>
<td><b>Rzadkość</b></td>
</tr>
<% #bialawiecej.id do |b| %>
<tr>
<td><%= b.nazwa %></td>
<td><%= b.obrazenia %>%</td>
<td><%= b.typ %></td>
<td><%= b.waga %></td>
<td><%= b.zasieg %></td>
<td><%= b.szybkosc %></td>
<td><%= b.rzadkosc %></td>
</tr>
<% end %>
</div>
</div>
On click send id of clicked item (GET). you will have link similar to : localhost:3000/desired_model/5
then in action do #desired_model = DesiredModel.find(params[:id])
redirect user to desired show page.
Show data.
Next time please provide some code :)

RoR: how make query to return the sum values for each group?

been teaching myself how to build a simple ruby/rails site. trying now to figure out the caveats for data manipulation. group is working, how do i "sum" all the columns?
index.html.erb
<table>
<tr>
<th>Site</th>
<th><7days</th>
<th>>7days</th>
<th>>30days</th>
<th>>90days</th>
<th>>180days</th>
<th>>365days</th>
</tr>
<tr>
<% #gdcomets.each do |dcomet| %>
<td><%= dcomet.site %></td>
<td><%= #sum1 %></td>
<td><%= #sum2 %></td>
<td><%= #sum3 %></td>
<td><%= #sum4 %></td>
<td><%= #sum5 %></td>
<td><%= #sum6 %></td>
</tr>
<% end %>
</table>
dcomets_controller
class DcometsController < ApplicationController
# GET /dcomets
# GET /dcomets.json
def index
#dcomets = Dcomet.all
#gdcomets = Dcomet.group(:site)
#sum1 = #gdcomets.map(&:ls7day).sum
#sum2 = #gdcomets.map(&:gt7day).sum
#sum3 = #gdcomets.map(&:gt30day).sum
#sum4 = #gdcomets.map(&:gt90day).sum
#sum5 = #gdcomets.map(&:gt180day).sum
#sum6 = #gdcomets.map(&:gt365day).sum
respond_to do |format|
format.html # index.html.erb
format.json { render json: #gdcomets }
end
end
end
#sum = #gdcomets.map(&:data).sum
:data being the name of the column you want to sum.
You'll get better performance letting SQL do the donkey work of summing everything:
Dcomet.group(:site).sum(:data)
Where :data is the name of column you want to sum. That will return a hash with the keys being the site values, and the values the corresponding sum for that site

Add link to methods to array in controller

I have a index method in my rails 4 application controller that looks like:
def index
#products = Product.all
#headers = #products.map(&:data).flat_map(&:keys).uniq
#product_data = #products.map{ |product| product[ :data ].values }
end
So #product_data ends up with something like:
[["Table", "$199.99", "blue"], ["Book", "$9.99", "green"]]
In my view, I put all of this in a unordered list. But now I'd like to have a link_to an edit and delete page for each product. How can I include this in my array, so I can display a link for each product on the view page?
You could add product_id to the result of product[:data].values array. Then use that product_id as parameter to your product url_helpers.
#product_data = products.map{ |product| product[ :data ].values.unshift(product.id) }
This should give you something similar to:
[[1, "Table", "$199.99", "blue"], [2, "Book", "$9.99", "green"]]
I see there is no use of #product_data there.Why can't You display the data in a table in your index.html.erb and you can loop through every product,so that the edit and delete links appear to every product.Assuming that you have name,price and color attributes for your product model,just do like this
In your index.html.erb:
<table border=1>
<tr>
<th>Product Name</th>
<th>Product Price</th>
<th>Product Color</th>
<th></th>
<th></th>
</tr>
<% #products.each do |p| %>
<tr>
<td><%=p.name %></td>
<td><%=p.price %></td>
<td><%=p.color %></td>
<td><%=link_to 'Edit', :action => "edit", :id => p.id %></td>
<td><%=link_to 'Delete', :action => "delete", :id => p.id, :confirm => "Are you sure?" %></td>
</tr>
<% end %>
</table>
Note:
Its just an another approach.

With Gmaps4rails, how can I display a list of locations that are currently visible in the map view?

I am using gmaps4rails. I am trying to achieve the following: The map is showing locations with markers and the user can zoom/move his window of sight as he wishes. What I want is that as he does so, a list next to the map should dynamically update with all the locations that are currently visible.
I tried using the callback mechanism and getBounds to filter a data structure of what is visible, but without success.
How can I go about it?
Thanks a lot!
Julian
In my controller: class LocationsController < ApplicationController
def index
if (params[:sw_y] && params[:sw_x] && params[:ne_y] && params[:ne_x])
bounds = [ [params[:sw_x].to_f, params[:sw_y].to_f],
[params[:ne_x].to_f, params[:ne_y].to_f] ]
#locations_within_bounds = Location.within_bounds(bounds)
else
#locations_within_bounds = Location.all
end
#locations = Location.all
#json = Location.all.to_gmaps4rails
And in my view:
<%= gmaps("markers" => {"data" => #json, "options" => {"list_container" => "markers_list", "do_clustering" => true } } ) %>
<table>
<tr>
<th>Name</th>
<th>Address</th>
<th>Longitude</th>
<th>Latitude</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #locations_within_bounds.each do |location| %>
<tr>
<td><%= location.name %></td>
<td><%= location.address %></td>
<td><%= location.longitude %></td>
<td><%= location.latitude %></td>
</tr>
<% end %>
</table>

Resources