Responsive Details Not Showing On jquery-datatables-rails - ruby-on-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>

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>

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>

How to display a list based on date in 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 %>

how do i display an index and a form on my show page?

How do I have my User's (users model) show page display a list of the Serivces (another model) they are offering AND also a form where people can book Appointments (another model) for their Services?
It looks like I need to make the #services variable more flexible in my controller. But I'm not sure how. It seems like only one partial I am rendering is able to make use of it in my controller. Weird!
USERS CONTROLLER
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
#appointment = Appointment.new
#services = #user.services.collect {|service| [ service.id, service.title, service.description, service.length, service.user_id] }
end
end
USERS/SHOW.HTML.ERB:
LIST OF SERVICES PARTIAL: <% render 'shared/services_list' %>
<table>
<thead>
<tr>
<th>Start time</th>
<th>End time</th>
<th>Note</th>
<th>Service</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #appointments.each do |appointment| %>
<tr>
<td><%= appointment.start_time %></td>
<td><%= appointment.end_time %></td>
<td><%= appointment.note %></td>
<td><%= appointment.service.title %></td>
<td><%= link_to 'Show', appointment %></td>
<td><%= link_to 'Edit', edit_appointment_path(appointment) %></td>
<td><%= link_to 'Destroy', appointment, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
USERS/SHOW.HTML.ERB:
APPOINTMENTS FORM PARTIAL: <%= render 'appointments/form' %>
<table>
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th>Length</th>
<th>User</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #services.each do |service| %>
<tr>
<td><%= service.title %></td>
<td><%= service.description %></td>
<td><%= service.length %></td>
<td><%= service.user.email %></td>
<td><%= link_to 'Show', service %></td>
<td><%= link_to 'Edit', edit_service_path(service) %></td>
<td><%= link_to 'Destroy', service, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
ERROR:
NoMethodError in Users#show
undefined method `title' for [1, "exmaple title", "example description", 90, 1]:Array
Line 15 is highlighted -> <%= service.title %>
<tbody>
<% #services.each do |service| %>
<tr>
<td><%= service.title %></td>
<td><%= service.description %></td>
<td><%= service.length %></td>
<td><%= service.user.email %></td>
Thanks!
Okay. #peshkira was right. I neeeded to call the object.
So I added this line to my controller #services_list = #user.services.collect so I could create a list in the style of an 'index'.
My full controller looks like this.
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
#appointment = Appointment.new
#services = #user.services.collect {|service| [ service.title, service.id] }
#services_list = #user.services.collect
end
end
That will supply functionality for a nested form(A Service & an Appointment for that Service) and also pull the data for a list of Services.

Resources