How to display a list based on date in rails? - ruby-on-rails

I'm trying to build a real simple event listing app that list a bunch of event based on Dates. I'm fairly new and was wondering how would I display a list or table based on dates? This is what I have now.
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th>Link</th>
<th>Date</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #events.each do |event| %>
<tr>
<td><%= event.title %></td>
<td><%= event.description %></td>
<td><%= event.link %></td>
<td><%= event.date %></td>
<td><%= link_to 'Show', event %></td>
<td><%= link_to 'Edit', edit_event_path(event) %></td>
<td><%= link_to 'Destroy', event, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
I would like it display in a date format. For example all events under the date of February/21 will appear under that specific heading.
Thanks!

You can try something like this:
<% #events.group_by(&:date).each do |date, events| %>
<tr><td colspan="7"><h1><%= date %></h1></td></tr>
<% events.each do |event| %>
<tr>
<td><%= event.title %></td>
<td><%= event.description %></td>
<td><%= event.link %></td>
<td><%= event.date %></td>
<td><%= link_to 'Show', event %></td>
<td><%= link_to 'Edit', edit_event_path(event) %></td>
<td><%= link_to 'Destroy', event, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
<% end %>

Related

NoMethodError for nil:NilClass on Rails

I have a Model called Categories, which I wanna display. Therefore I have inputed the following statement in the pages/home.html.erb
<%= render 'categories/index.html.erb' %>
Now whenever I run the server I get a NoMethodError for the line :
<% #categories.each do |category| %>
This is the full index.html.erb file of the categories view:
<p id="notice"><%= notice %></p>
<h1>Categories</h1>
<table>
<thead>
<tr>
<th>Title</th>
<th>Price</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #categories.each do |category| %>
<tr>
<td><%= category.title %></td>
<td><%= category.price %></td>
<td><%= link_to 'Show', category %></td>
<td><%= link_to 'Edit', edit_category_path(category) %></td>
<td><%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Category', new_category_path %>
<p id="notice"><%= notice %></p>
<h1>Categories</h1>
<table>
<thead>
<tr>
<th>Title</th>
<th>Price</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #categories.each do |category| %>
<tr>
<td><%= category.title %></td>
<td><%= category.price %></td>
<td><%= link_to 'Show', category %></td>
<td><%= link_to 'Edit', edit_category_path(category) %></td>
<td><%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Category', new_category_path %>
<p id="notice"><%= notice %></p>
<h1>Categories</h1>
<table>
<thead>
<tr>
<th>Title</th>
<th>Price</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #categories.each do |category| %>
<tr>
<td><%= category.title %></td>
<td><%= category.price %></td>
<td><%= link_to 'Show', category %></td>
<td><%= link_to 'Edit', edit_category_path(category) %></td>
<td><%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Category', new_category_path %>
Can someone help me understand why this is not working?
When you want to render the categories/index.html.erb partial on your homepage too, then you must initialize all variables that are used in that partial.
Because you use #categories in that partial you will need to add the following to your controller action. I assume that you have a PagesController with a home method already.
# in app/controllers/pages_controller.rb
def home
#categories = Category.all # <= Add this line
end

Filtering the data showed on index page using parameter received in URL

I have two models here for Item and RecipeIngredient as below where RecipeIngredient belongs to Item.
Model for Item
class Item < ApplicationRecord
belongs_to :item_type, :class_name=>ItemType, :foreign_key=>"item_type_id"
end
Model for RecipeIngredient
class RecipeIngredient < ApplicationRecord
belongs_to :item, :class_name=>Item, :foreign_key=>"item_id"
belongs_to :ingredient, :class_name=>Ingredient, :foreign_key=>"ingredient_id"
validates_numericality_of :quantity
end
I have index page for Items where I have created a link to index page for RecipeIngredients and I am passing the id of the item in the URL as parameter.
Index Page for Item
<p id="notice"><%= notice %></p>
<h1>Items</h1>
<table>
<thead>
<tr>
<th>Item</th>
<th>Item type</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #items.each do |item| %>
<tr>
<td><%= item.item %></td>
<td><%= item.item_type.item_type %></td>
<td><%= link_to 'Show', item %></td>
<td><%= link_to 'Edit', edit_item_path(item) %></td>
<td><%= link_to 'Destroy', item, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<td><%= link_to 'Add Recipe', recipe_ingredients_path(:item_id =>item.id) %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Item', new_item_path %>
<%= link_to "Home", '/' %>
And I have the controller for RecipeIngredients as below.
Controller for RecipeIngredient
def index
#recipe_ingredients = RecipeIngredient.find(params[:item_id])
end
And the index page for recipe ingredients looks like as given below. I need to filter the data displayed on this index page of recipe ingredients only to match the item_id received as a parameter in URL.
Index for RecipeIngredients
<p id="notice"><%= notice %></p>
<h1>Recipe Ingredients</h1>
<table>
<thead>
<tr>
<th>Item</th>
<th>Ingredient</th>
<th>Quantity</th>
<th>Unit</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #recipe_ingredients.each do |recipe_ingredient| %>
<tr>
<td><%= recipe_ingredient.item.item %></td>
<td><%= recipe_ingredient.ingredient.ingredient %></td>
<td><%= recipe_ingredient.quantity %></td>
<td><%= recipe_ingredient.ingredient.recipe_unit %></td>
<td><%= link_to 'Show', recipe_ingredient %></td>
<td><%= link_to 'Edit', edit_recipe_ingredient_path(recipe_ingredient) %></td>
<td><%= link_to 'Destroy', recipe_ingredient, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Recipe Ingredient', new_recipe_ingredient_path %>
<%= link_to 'Back', '/items' %>
Right now I am getting this error:
undefined method `show' for #< RecipeIngredient:0xa5653b8>
#recipe_ingredients = RecipeIngredient.find(params[:item_id])
This will return you only one RecipeIngredient with id = params[:item_id] not activerecord array
So you need to change find to where if you want to loop over #recipe_ingredients
#recipe_ingredients = RecipeIngredient.where(params[:item_id])
or, you need to change the view
<tbody>
<tr>
<td><%= #recipe_ingredients.item.item %></td>
<td><%= #recipe_ingredients.ingredient.ingredient %></td>
<td><%= #recipe_ingredients.quantity %></td>
<td><%= #recipe_ingredients.ingredient.recipe_unit %></td>
<td><%= link_to 'Show', #recipe_ingredients %></td>
<td><%= link_to 'Edit', edit_recipe_ingredient_path(#recipe_ingredients) %></td>
<td><%= link_to 'Destroy', #recipe_ingredients, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
</tbody>

Responsive Details Not Showing On jquery-datatables-rails

I've been trying to make this work and I can't seem to find a solution:
I have a table and I have a basic initialization of the Datatables everything seems to work fine but once the responsive gets activated the details button doesn't work if I click on it it goes red with the - sign but the details doesn't appear.
This is my table:
<table id="tabla" class="display dt-responsive no-wrap" width="100%">
<thead>
<tr>
<th>Profesor</th>
<th>Materia</th>
<th>Seccion</th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% #profesors.each do |profesor| %>
<tr>
<td><%= profesor.profesor %></td>
<td><%= profesor.materia %></td>
<td><%= profesor.seccion %></td>
<td><%= link_to 'Show', profesor %></td>
<td><%= link_to 'Edit', edit_profesor_path(profesor) %></td>
<td><%= link_to 'Destroy', profesor, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<td><%= link_to 'Alumnos', alumnos_path("prof" => profesor) %></td>
</tr>
<% end %>
</tbody>
</table>
This is my JS
$('#tabla').DataTable({
responsive: true
});
If you use bootstrap try wrapping the <table> with <div class="table-responsive"> link
Some like:
<div class="table-responsive">
<table id="tabla" class="display dt-responsive no-wrap" width="100%">
<thead>
<tr>
<th>Profesor</th>
<th>Materia</th>
<th>Seccion</th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% #profesors.each do |profesor| %>
<tr>
<td><%= profesor.profesor %></td>
<td><%= profesor.materia %></td>
<td><%= profesor.seccion %></td>
<td><%= link_to 'Show', profesor %></td>
<td><%= link_to 'Edit', edit_profesor_path(profesor) %></td>
<td><%= link_to 'Destroy', profesor, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<td><%= link_to 'Alumnos', alumnos_path("prof" => profesor) %></td>
</tr>
<% end %>
</tbody>
</table>
</div>

Rails when I wrap a table with a form the table disappears checkbox

I am not sure what is happening here. When I wrap the form around the table the table disappears. When I remove the form, the table reappears. Thanks in advance. Here is the code:
<% form_tag deposit_checks_path :method => :put do %>
<table>
<thead>
<tr>
<th>Checkbox</th>
<th>Date</th>
<th>Mailer ID</th>
<th>Payment amt</th>
<th>Transaction type</th>
<th>Transaction</th>
<th>Deposit</th>
<th>User</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #payments.each do |payment| %>
<tr>
<td><%= check_box_tag "payment_id[]", payment.id, checked = false %></td>
<td><%= payment.created_at %></td>
<td><%= payment.mailer_id %></td>
<td><%= number_to_currency(payment.payment_amt) %></td>
<td><%= payment.transaction_type %></td>
<td><%= payment.transaction_id %></td>
<td><%= payment.deposit_id %></td>
<td><%= payment.user_id %></td>
<td><%= link_to 'Show', payment %></td>
<td><%= link_to 'Edit', edit_payment_path(payment) %></td>
<td><%= link_to 'Destroy', payment, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<%= submit_tag "Edit Checked" %>
<% end %>
thanks for the help
did you notice in your code?
<% form_tag deposit_checks_path :method => :put do %>
you forgot to add a comma. It should have been
<% form_tag deposit_checks_path, :method => :put do %>
otherwise the method: :put is appended to the action attribute
Main Point:
You forgot to put an equals sign before form_tag
Try this
<%= form_tag deposit_checks_path, :method => :put do %>
Try like this
<table>
<thead>
<tr>
<th>Checkbox</th>
<th>Date</th>
<th>Mailer ID</th>
..
..
</tr>
</thead>
<tbody>
<tr>
<td>
<% form_tag deposit_checks_path :method => :put do %>
<table>
<% #payments.each do |payment| %>
<tr>
<td><%= check_box_tag "payment_id[]", payment.id, checked = false %></td>
<td><%= payment.created_at %></td>
<td><%= payment.mailer_id %></td>
...
...
</tr>
<%end%>
</table>
<% end %>
</td>
</tr>
</tbody>
</table>

Number the result of views in Ruby on Rails

I am getting data in from a controller and its printed through a loop. This is the example from the getting started guide. I want each row to have class in numbered in order like, class-1 class-2 etc.
Below is my code in view file.
<% #posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.text %></td>
<td><%= link_to 'Show', post_path(post) %> | </td>
<td><%= link_to 'Edit', edit_post_path(post) %> | </td>
<td><%= link_to 'Delete', post_path(post),
method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
For example each row should be having classes in order like below when rendered:
<tr class="class-1">
<td>title</td>
</tr>
<tr class="class-2">
<td>title</td>
</tr>
<tr class="class-3">
<td>title</td>
</tr>
Something like
<% classid = 0 %>
<% #posts.each do |post| %>
<%= "<tr class=\"class-#{classid}\">" %>
<td><%= post.title %></td>
<td><%= post.text %></td>
<td><%= link_to 'Show', post_path(post) %> | </td>
<td><%= link_to 'Edit', edit_post_path(post) %> | </td>
<td><%= link_to 'Delete', post_path(post),
method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% classid +=1 %>
<% end %>
That said I'd be looking at presenter pattern, or a method on whatever class post is or better still if post is in the db, using it's id instead of sequential number.

Resources