How can I fix Blank arrays on Ruby on Rails views? - ruby-on-rails

I am trying to display some information in my views, but it doesn't return any information and it appears completely blank in the view
that's my controller:
class Spree::Admin::StocklogController < Spree::Admin::BaseController
before_action :load_movements
def index
end
def load_movements
#stockmovelog = Spree::StockMovement.all
end
end
And that's my view:
<% #stockmovelog.each do |movement| %>
<tr>
<td><% movement.id %></td>
<td class="align-center"><% movement.quantity %></td>
<td><% movement.created_at %></td>
<td class="align-right"><% movement.updated_at %></td>
<td class="actions"><% movement.action %></td>
</tr>
<% end %>
It will simply create the number of rows from each and keep the content blank, there is something I might be doing wrong?

Just add = to the ERB inside the td's:
<% #stockmovelog.each do |movement| %>
<tr>
<td><%= movement.id %></td>
<td class="align-center"><%= movement.quantity %></td>
<td><%= movement.created_at %></td>
<td class="align-right"><%= movement.updated_at %></td>
<td class="actions"><%= movement.action %></td>
</tr>
<% end %>

Related

how to write each loop for has_many in rails?

employee has many skills
skill belongs_to employee
Skills table have employee_id(foreign key).
I want to display in view
There are 4 ways to get the collections of users.
#employees = Employee.all.joins(:skills)
#employees = Employee.all.includes(:skills)
#employees = Employee.all.eager_load(:skills)
In your view add below code in HTML/HAML/SLIM file.
<% #employees.each do |employee| %>
<tr>
<td><%= employee.name %></td>
<td><%= employee.skills.map(&:title).join(", ") %></td>
</tr>
<% end %>`
You could try with something like this.
In controller return all the employees
so it will be like
#employees = Employee.all.includes(:skills)
and in the view you can do
<% #employees.each do |employee| %>
<tr>
<td><%= employee.name %></td>
<td><%= employee.skills.collect(&:title).join(", ") %></td>
</tr>
<% end %>
Best practice in this case is to write an instance method on your Employee class:
class Employee < ApplicationRecord
has_many :skills
def skills_list
skills.pluck(:title).join(', ')
end
end
And use it like this in your view:
<table>
<tr>
<td><%= #employee.id %></td>
<td><%= #employee.name %></td>
<td><%= #employee.skills_list %></td>
<tr>
</table>
You can use:
<table>
<tr>
<th>Employee </th>
<th>Skills</th>
</tr>
<% #employees.each do |employee|%>
<tr>
<td><%= employee.name %></td>
<td><%= employee.skills.map(&:name).join(', ') %></td>
</tr>
<% end %>
</table>
This above code will list all skill names of each employee.
You can read a complete guide here

has_many loop undefined method

I have a has_many association in which I want to display in a table. Each Agent can make up to 3 sales, but I want to list them in the same row as the Agent.
undefined method `each' for nil:NilClass on the <% sales.each do |s| %> line.
I'm getting the above error, event though there is defiantly data present for each column
<table>
<tr>
<td>Agent</td>
<td>Date</td>
<td>Product</td>
<td>Price</td>
<td>Date</td>
<td>Product</td>
<td>Price</td>
<td>Date</td>
<td>Product</td>
<td>Price</td>
</tr>
<% #agent.sales.each do |agent, sales| %>
<tr>
<td><% agent.name %></td>
<% sales.each do |s| %>
<td><%= s.date %></td>
<td><%= s.product %></td>
<td><%= s.price %></td>
<% end %>
</tr>
<% end %>
</table>
To start, since #agent.sales is an ActiveRecord_Associations_CollectionProxy, you can think of this object more like an array, rather than a hash.
How you initially wrote your loop it appears you were treating it like a hash:
#agent.sales.each do |agent, sale| > this syntax is saying for each key-value pair in the hash, do something with the key (agent) and value (sale).
The second time you try to run a loop, you're running it on the "value" part of the iterator, which does not exist, hence the error on the line sales.each do |sale|
Really, you are very close but are overthinking it. Just drop your first iterator and keep the rest:
<tr>
<td><% #agent.name %></td>
<% #agent.sales.each do |s| %>
<td><%= s.date %></td>
<td><%= s.product %></td>
<td><%= s.price %></td>
<% end %>
</tr>
By the way, you can apply the same principle and refactor your headers too:
<tr>
<td>Name</td>
<% 3.times do |n| %>
<td>Date</td>
<td>Product</td>
<td>Price</td>
<% end %>
</tr>
This assumes that your constraint is 3 max sales.
This should work:
<table>
<tr>
<td>Agent</td>
<td>Date</td>
<td>Product</td>
<td>Price</td>
<td>Date</td>
<td>Product</td>
<td>Price</td>
<td>Date</td>
<td>Product</td>
<td>Price</td>
</tr>
<% #agent.sales.each do |sale| %>
<tr>
<td><% #agent.name %></td>
<td><%= sale.date %></td>
<td><%= sale.product %></td>
<td><%= sale.price %></td>
</tr>
<% end %>
</table>

render all records in a stored procedure

I am trying to render all the data of a stored procedure in a table in my view but the problem is that the name of the columns can change in each stored query so I can not make the specific field call in the render because each query brings different columns, how could I make the call of the columns name for the (table titles) and render all the data in the unspecified in the name of the data but bring all the results of the query that apparently returns them in an array?
this is my stored_procedures_controller.rb
class StoredProceduresController < ApplicationController
before_action :authenticate_usuario!
def cantidad_productosxpedido
#cantidad_productosxpedido = Stored_procedure.fetch_db_records("exec SPAD_ReporteProductosxPedido '#{Time.now.try(:strftime,"%Y%m%d")}','#{params[:search]}','#{params[:search6]}'")
respond_to do |format|
format.html
format.js
format.xls
end
end
end
this is my model stored_procedure.rb
class Stored_procedure < ActiveRecord::Base
def self.fetch_db_records(proc_name_with_parameters)
connection.select_all(proc_name_with_parameters)
end
def self.insert_update_delete_calculate(proc_name_with_parameters)
connection.execute(proc_name_with_parameters)
end
def self.fetch_val_from_sp(proc_name_with_parameters)
connection.select_value(proc_name_with_parameters)
end
end
I usually do the rendering of the stored this way
<table class="table table-striped table-bordered table-condensed table-hover rwd_auto">
<thead>
<tr>
<th>Ruta</th>
<th>Pedido</th>
<th>IdCliente</th>
<th>Cliente</th>
<th>Nombre Comercial</th>
<th>2010100</th>
<th>2020100</th>
<th>2020200</th>
<th>2020500</th>
<th>2021200</th>
<th>2200500</th>
<th>2200501</th>
<th>2200508</th>
<th>2200509</th>
<th>2201000</th>
<th>2203100</th>
<th>2203101</th>
<th>2203102</th>
<th>2203104</th>
<th>2203108</th>
<th>2203200</th>
</tr>
</thead>
<tbody id="container_productosxpedido">
<%= render partial: "/stored_procedures/cantidad_productoxpedido", collection: #cantidad_productosxpedido %>
</tbody>
</table>
and in the partial _cantidad_productoxpedido
<tr>
<td><%= cantidad_productoxpedido['Ruta'] %></td>
<td><%= cantidad_productoxpedido['Pedido'] %></td>
<td><%= cantidad_productoxpedido['IdCliente'] %></td>
<td><%= cantidad_productoxpedido['Cliente'] %></td>
<td><%= cantidad_productoxpedido['NombreCorto'] %></td>
<td><%= cantidad_productoxpedido['2010100'] %></td>
<td><%= cantidad_productoxpedido['2020100'] %></td>
<td><%= cantidad_productoxpedido['2020200'] %></td>
<td><%= cantidad_productoxpedido['2020500'] %></td>
<td><%= cantidad_productoxpedido['2021200'] %></td>
<td><%= cantidad_productoxpedido['2200500'] %></td>
<td><%= cantidad_productoxpedido['2200501'] %></td>
<td><%= cantidad_productoxpedido['2200508'] %></td>
<td><%= cantidad_productoxpedido['2200509'] %></td>
<td><%= cantidad_productoxpedido['2201000'] %></td>
<td><%= cantidad_productoxpedido['2203100'] %></td>
<td><%= cantidad_productoxpedido['2203101'] %></td>
<td><%= cantidad_productoxpedido['2203102'] %></td>
<td><%= cantidad_productoxpedido['2203104'] %></td>
<td><%= cantidad_productoxpedido['2203108'] %></td>
<td><%= cantidad_productoxpedido['2203200'] %></td>
</tr>
but when changing the name of the columns I do not know how to do to display the data without specifying the names of those columns
UPDATE
I could resolve to display the contents of the table as follows
<%#cantidad_productosxpedido.to_a.each do |foo|%>
<tr>
<%foo.each do |label, value|%>
<td><%= value %></td>
<%end%>
</tr>
<%end%>
but now I try to add the headers of the table
like this:
<%#cantidad_productosxpedido.first.each do |foo|%>
<tr>
<%foo.each do |label, value|%>
<th><%= label %></th>
<%end%>
</tr>
<%end%>
but it does not show correct in the table but renders it as follows
<tr>
<th>title1</th> <td>value1</th>
</tr>
<tr>
<th>title2</th> <th>value2</th>
</tr>
object.attributes.keys will return an array of column names for a given record. You can use that to generate your columns as follows:
<% #cantidad_productosxpedido.each do |cantidad_productoxpedido| %>
<% cantidad_productoxpedido.keys.each do |col| %>
<td><%= cantidad_productoxpedido[col] %></td>
<% end %>
<% end %>
Not tested, but that should work.

Rails 5 controller dumping data from database as an array

I'm just starting out in learning Ruby on Rails. I have the following code in movies_controller.
class MoviesController < ApplicationController
def index
#movies = Movie.all
end
end
and my index.html.erb has a table structure to display the data from the database as follows:
<h4><%= pluralize(#movies.size, 'Movie') %> Found</h4>
<table>
<tr>
<th>Movie Title</th>
<th>Rating</th>
<th>Gross Revenue</th>
</tr>
<%= #movies.each do |movie| %>
<tr>
<td><%= movie.title %></td>
<td><%= movie.rating %></td>
<td><%= number_to_currency(movie.total_gross) %></td>
</tr>
<% end %>
</table>
The index.html.erb first dumped the whole movies data as an array and then display the data below it in the table structure shown above. Please what have I got wrong? Thanks.
Remove the = sign from the each block
<% #movies.each do |movie| %>
<tr>
<td><%= movie.title %></td>
<td><%= movie.rating %></td>
<td><%= number_to_currency(movie.total_gross) %></td>
</tr>
<% end %>
You have a extra = when you start the loop. That page should look like
<h4><%= pluralize(#movies.size, 'Movie') %> Found</h4>
<table>
<tr>
<th>Movie Title</th>
<th>Rating</th>
<th>Gross Revenue</th>
</tr>
<% #movies.each do |movie| %>
<tr>
<td><%= movie.title %></td>
<td><%= movie.rating %></td>
<td><%= number_to_currency(movie.total_gross) %></td>
</tr>
<% end %>
</table>

How to get unique elements through array and group them in ruby on rails

i have issue. This below code is the views code of ruby in rails
<table >
<tr>
<th>Url</th>
<th>Tags</th>
</tr>
<% #array_bookmark = #bookmark.class == Array ? #bookmark : [#bookmark] %>
<% Array(#bookmark).each do |book| %>
<tr>
<td><%= book.url %></td>
<td><%= book.tags %></td>
</tr>
<% end %>
</table>
This yields something like :
Url Tags
www.mrabhiram.tumblr.com abhi
www.mrabhiram.tumblr.com blog
google.com google
google.com blog
But, i want to get it as
Url Tags
www.mrabhiram.tumblr.com abhi,blog
google.com google,blog
Can anyone provide me the solution? It should be generic enough to iterate over the array.
Thank you in advance.
<% Array(#bookmark).group_by {|b| b.url}.each do |url, books| %>
<tr>
<td><%= url %></td>
<td><%= books.map {|b| b.tags}.flatten.uniq.join(" ") %></td>
</tr>
<% end %>
Use group_by statement
upd
<% Array(#bookmark).group_by(&:url).each do |url, books| %>
<tr>
<td><%= url %></td>
<td><%= books.map(&:tags).flatten.join(',') %></td>
</tr>
<% Array(#bookmark).uniq.each do |book| %>
<tr>
<td><%= book.url %></td>
<td><%= book.tags %></td>
</tr>
<% end %>
the above will work.

Resources