I want to display some data from a table and display it in the table cells. I have the following.
Controller
#data = HospitalBooking.where(:created_at => #date_range)
#stuff = []
#stuff[0] = []
index = 0
#stuffs.each do |stuff|
#rotated[0][index] = stuff.detail0
index += 1
end
Not sure if I have gone about this the correct way also how would I display it in my view
If I understood it right, you want to display the result of a query in a html table.
Here is your query, in your controller:
#data = HospitalBooking.where(:created_at => #date_range)
In your view
<table>
<tr>
<th>field1</th>
<th>field2</th>
</tr>
<% #data.each do |data| %> #start loop
<tr>
<td><%= data.field1 %></td> #column field1 in your database
<td><%= data.field2 %></td> #column field2 in your database
</tr>
<% end %> #end loop
</table>
Related
I'm putting together a combo box or called the same as a select in rails, I put everything together but it gives me an error that tells me that I have a problem with the map inside the select, I'm using simple_form_for and I'm doing a map inside the collection inside the selector or called in simple_for associatio.
I copy the view and the controller
This view
<h1>HistContact#index</h1>
<p>Find me in app/views/hist_contact/index.html.erb</p>
<%= simple_form_for #histcontact, url:hist_contact_index_path do |f| %>
<% f.association :contact, collection: #contacts.map{|cont| [cont.name , cont.id]}%>
<%f.submit "buscar"%>
<% end %>
<table id = "client_table" class="table table-striped table-sm">
<thead>
<tr>
<th>Id</th>
<th>fecha</th
</tr>
</thead>
<tbody>
<% #histcontacts.each do |c|%>
<tr>
<td> <%= c.id %> </td>
<td> <%= c.created_at %></td>
</tr>
<% end %>
</tbody>
</table>
the controller
class HistContactController < ApplicationController
before_action :authenticate_user!
def index
#histcontacts = HistContact.all
#contacts = Contact.all
end
def new
#histcontact = HistContact.new
#contacts = Contact.new
end
def create
#histcontact = HistContact.find(contact_id: params[:contact])
end
private
def contactID(current_user)
client_id = Client.where(user_id: current_user.id)
contact_id = Contact.where(client_id: client_id.ids[0])
return contact_id
end
end
Thank you
According to error, you are trying to map a single object instead of an array of objects. Based on your controller code, the view file you shared is probably new.html.erb. To solve this problem you need do it like this:
def new
#histcontact = HistContact.new
#contacts = Contact.all
end
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
I think this is simple but I'm just not getting it. I need to join 3 tables and have rails return values from all 3. I have done a join but rails only returns values from 1 table. I think I just don't know the syntax
Here is my controller method
#previous_results = Test.joins(:run, :detail).where("runs.name = ? AND tests.name = ?", #run.name, #test.name).last(1000)
That does do a join here is the sql from it
SELECT `tests`.* FROM `tests` INNER JOIN `runs` ON `runs`.`id` = `tests`.`run_id` INNER JOIN `details` ON `details`.`test_id` = `tests`.`id`
WHERE (runs.name = 'SNMSubscriberSpecificTemplatesFeedsTest' AND tests.name = 'testSitePrefixFalseForNTINSequentialList') ORDER BY `tests`.`id` DESC LIMIT 1000
all I need to do is return values from the details table also. I want 2 column values from details essentially instead of doing test .* I was test.id, test.name, detail.text, detail.message as my return
Int active record how do I tell it to not just return test table data but also include columns in details
This query in SQL does what I want I need to know how to active record it
SELECT `tests`.*, `details`.`detail_text`, `details`.`detail_error`
FROM `tests`
INNER JOIN `runs` ON `runs`.`id` = `tests`.`run_id`
INNER JOIN `details` ON `tests`.`id` = `details`.`id`
WHERE (runs.name = 'SNMSubscriberSpecificTemplatesFeedsTest' AND tests.name = 'testSitePrefixFalseForNTINSequentialList')
ORDER BY `tests`.`id` DESC LIMIT 1000
#tests = Test
.select('tests.*', 'details.detail_text', 'details.detail_error')
.joins(:run, :detail)
.where(
runs: { name: 'SNMSubscriberSpecificTemplatesFeedsTest' },
tests: { name: 'testSitePrefixFalseForNTINSequentialList' }
)
.limit(1000)
.order(id: :desc)
your views would look like:
<table>
<tr>
<th>Test ID</th>
<th>Test Detail's Detail Text</th>
<th>Test Detail's Detail Error</th>
</tr>
<% #tests.each do |test| %>
<tr>
<td><%= test.id %></td>
<td><%= test.detail_text %></td>
<td><%= test.detail_error %></td>
</tr>
<% end %>
</table>
Recommendation:
do not use select() explicitly to fetch the "associated values", but instead access them directly through the objects, like below
#tests = Test
.joins(:run, :detail).includes(:detail)
.where(
runs: { name: 'SNMSubscriberSpecificTemplatesFeedsTest' },
tests: { name: 'testSitePrefixFalseForNTINSequentialList' }
)
.limit(1000)
.order(id: :desc)
your views would look something like:
<table>
<tr>
<th>Test ID</th>
<th>Test Detail's Detail Text</th>
<th>Test Detail's Detail Error</th>
</tr>
<% #tests.each do |test| %>
<tr>
<td><%= test.id %></td>
<td><%= test.detail.detail_text %></td>
<td><%= test.detail.detal_error %></td>
</tr>
<% end %>
</table>
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
could anyone tell me why does my rails application returns this piece of code when I do a select statement?
#<Driver:0x22ef3f0>
The select statement is:
current_schedule_record = {
'driver_name' => Driver.where(['id = ?', id]).select('first_name').first
}
and the view is:
<% #trucks.each do |truck| %>
<% record = ScheduleController.schedule_record(truck.id) %>
<tr>
<td><%= truck.id %></td>
<td><%= record['driver_name'] %></td>
</tr>
<% end %>
From the documentation
Be careful because this also means you're initializing a model object with only the fields that you've selected.
So using select still creates a model object. You need to use something like
current_schedule_record = {
'driver_name' => Driver.where(['id = ?', id]).select('first_name').first.first_name
}