Rails 5 controller dumping data from database as an array - ruby-on-rails

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>

Related

How can I fix Blank arrays on Ruby on Rails views?

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 %>

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>

How to make a nested table in rails

I want to create a table named groups that lists of each group. Inside of each group, I want to list the groups courses. The group has_many :courses, and the course belongs_to :group. My current attempt does not work, and I have no idea where to go from there
Current Attempt
courses_controller.rb
def index
#courses = Course.find_by(group: :group_id)
#groups = Group.all
end
index.html.erb
<table>
<thead>
<tr>
<th>Title</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #groups.each do |group| %>
<tr>
<td><%= group.title %></td>
<td><%= link_to 'Show', group %></td>
<td><%= link_to 'Edit', edit_group_path(group) %></td>
<td><%= link_to 'Destroy', group, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<tr>
<td>
<table>
<thead>
<tr>
<th>Title</th>
</tr>
</thead>
<tbody>
<% #courses.each do |course| %>
<tr>
<% if Lesson.exists?(user: current_user, course: course) %>
<td><%= link_to course.title, edit_lesson_path(Lesson.find_by(user: current_user, course: course)) %></td>
<% else %>
<td><%= link_to course.title, new_course_lesson_path(course) %></td>
<% end %>
<% if current_user.master? %>
<td><%= link_to 'Edit', edit_course_path(course) %></td>
<td><%= link_to 'Destroy', course, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<% else %>
<% end %>
</tr>
<% end %>
</tbody>
</table>
<br>
<% if current_user.master? %>
<%= link_to 'New Course', new_course_path %>
<% end %></td>
</tr>
<% end %>
</tbody>
</table>
Any help is appreciated :)
UPDATE
:group_id is the groups id for each created course, since the group has_many :courses The lesson belongs_to :course but that isnt important, as I just want the table, Thanks XP
As I said in my comment, what do you mean it isn't working? Are you getting any error messages? How have you structured you models? Have you created a migration to add group id to courses? What does your routes file look like - are courses nested under groups? If you don't provide this kind of information, its difficult to provide help.
If you look in the logs, you'll get some messages which will direct you to where things are going wrong. Have you tried running
Course.find_by(group: :group_id)
in a rails console. I suspect you'll find that this line is causing problems. Comment out this line and amend your erb as follows (I've not included all the code, but just to give you an idea). Also its not a good idea to use tables within tables.
<table>
<thead>
<tr>
<th>Title</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #groups.each do |group| %>
<tr>
<td><%= group.title %></td>
</tr>
<tr>
<td>
<table>
<thead>
<tr>
<th>Title</th>
</tr>
</thead>
<tbody>
<% #group.courses.each do |course| %>
<tr>
<% if Lesson.exists?(user: current_user, course: course) %>
<td><%= link_to course.title, edit_lesson_path(Lesson.find_by(user: current_user, course: course)) %></td>
<% else %>
<td><%= link_to course.title, new_course_lesson_path(course) %></td>
<% end %>

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.

Trying not to repeat myself with Rails data manipulation

I'm fairly new to rails, an an intermediate programmer in general, but I'll try to be as clear as I can.
I have a database table for Products, which owns a field called category. Category has two possible values, call them Category1 and Category 2. The goal is to retrieve all the Products and display them in HTML organized by Category. I can make this work, but I know there has to be a better way.
My method now is to get my Products like so:
#category1_products = Product.all(conditions: { category: "Category1" })
#category2_products = Product.all(conditions: { category: "Category2" })
and then output the data something like this:
<table>
<tr>
<td>Category1 Name</td>
<% #category1_products.each do |product| %>
<td><%= product.name %></td>
<td><%= product.description %></td>
<td><%= product.price %></td>
<% end %>
</tr>
<tr>
<td>Category2 Name</td>
<% #category2_products.each do |product| %>
<td><%= product.name %></td>
<td><%= product.description %></td>
<td><%= product.price %></td>
<% end %>
</tr>
</table>
I would like to accomplish this using a single instance: get all Products at once, group them by Category, and then output them by looping through one Category at a time (including the Category Names). This seems like a pretty elementary concept to me, but I'm having trouble getting my head around it.
#products_by_category = Product.all.group_by(&:category)
This is a Ruby (read: not ActiveRecord) method to turn an Array into a Hash based on some condition of the objects it contains.
In your view (assuming you want one row per product, and not one per category):
<table>
<% #products_by_category.each do |category,products| %>
<tr>
<th><%= category %></th>
</tr>
<% products.each do |product| %>
<tr>
<td><%= product.name %></td>
<td><%= product.description %></td>
<td><%= product.price %></td>
</tr>
<% end %>
<% end %>
</table>
Rails' group_by is great for this type of problem:
#grouped_products = Product.all.group_by(&:category)
and then in the view, just loop over #grouped_products:
<table>
<tr>
<% #grouped_products.each do |category, products| %>
<td><%= category %></td>
<% products.each do |product| %>
<td><%= product.name %></td>
<td><%= product.description %></td>
<td><%= product.price %></td>
<% end %>
<% end %>
</tr>
</table>

Resources