Error when delete comment Ruby on Rails - ruby-on-rails

When I delete comment I see
undefined method 'each' for nil:NilClass
Also, the user cannot delete his account if has posted... It makes the error.
This is code
<p id="notice"><%= notice %></p>
<h1>Comments</h1>
<table>
<thead>
<tr>
<th>Link</th>
<th>Body</th>
<th>User</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #comments.each do |comment| %>
<tr>
<td><%= comment.link_id %></td>
<td><%= comment.body %></td>
<td><%= comment.user %></td>
<td><%= link_to 'Show', comment %></td>
<td><%= link_to 'Edit', edit_comment_path(comment) %></td>
<td><%= link_to 'Destroy', comment, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Comment', new_comment_path %>

Your are not checking if any comments are present before looping through them. So if there are no comments you are calling each on nil.
This should fix it
<tbody>
<% if #comments.any? %> <----- Add in this line and the end below
<% #comments.each do |comment| %>
<tr>
<td><%= comment.link_id %></td>
<td><%= comment.body %></td>
<td><%= comment.user %></td>
<td><%= link_to 'Show', comment %></td>
<td><%= link_to 'Edit', edit_comment_path(comment) %></td>
<td><%= link_to 'Destroy', comment, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
<% end %> <---- here
</tbody>

Related

submit_tag with check_box_tag returning error

I'm having some trouble with check_box_tag and my method enable (a product can be disabled) in Ruby on Rails.
I created some checkbox so the user select the products that he wants to set enable = true.
Keeps returning the error "Couldn't find Product with 'id'=enable"
My method enable:
def enable
Product.update_all(["enable=?", true], :id => params[:selected_products])
redirect_to root_url
end
My routes.rb:
Rails.application.routes.draw do
get 'product_export/new'
get 'product_imports/new'
resources :users
resources :product_imports
resources :products do
collection do
post :import
post :export
post :enable
end
end
root to: 'products#index'
end
and finally my view with the table e the check_box_tag:
<%= form_tag enable_products_path, :method => :put do %>
<%= submit_tag "Enable products" %>
<table class="table table-striped table-responsive" id="productsTable">
<thead class="thead-inverse">
<tr>
<th/>
<th>Code</th>
<th>Enable</th>
<th>Bar code</th>
<th>Unit cost</th>
<th>Description</th>
<th>Family</th>
<th>Final</th>
<th>Measure</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #products.each do |product| %>
<tr>
<td><%= check_box_tag 'selected_products[]', product.id %></td>
<td><%= link_to (product.code), edit_product_path(product) %></td>
<td><%= product.enable %></td>
<td><%= product.bar_code %></td>
<td><%= product.unit_cost %></td>
<td><%= product.description %></td>
<td><%= product.family %></td>
<td><%= product.final %></td>
<td><%= product.measure %></td>
<td><%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
Thanks for the help!
Use where to find selected products and when to update them:
Product.where(id: params[:selected_products]).update_all(enable: true)

Rails Error comparing object with parameter inside view

I have my index.html like this:
<% #alumno_inscriptos.each do |alumno_inscripto| %>
<tr>
<% if (alumno_inscripto.clase_id) == (params[:id]) %>
<td><%= alumno_inscripto.clase_id %><td>
<td><%= params[:id] %><td>
<td><%= buscarNombre(alumno_inscripto.alumno_id).name %> <%= buscarNombre(alumno_inscripto.alumno_id).lastname %> </td>
<td> <%= alumno_inscripto.presencia %></td>
<td> <%= alumno_inscripto.pago %></td>
<td><%= link_to 'Destroy', alumno_inscripto, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<% else %>
<td><%= "no va. solo para testear" %><td>
<td><%= alumno_inscripto.clase_id %><td>
<td><%= params[:id] %><td>
<% end %>
</tr>
<% end %>
The problem is that inside the 'if', if the 2 parameters are the same, it always goes to the else part. I leave you an image of what it print. As you can see in the last row the values are the same. Do you have a solution for this?
Thank you!

How to obtain count of comments

This is my Articles index.html.erb so far
<table>
<tr>
<th>Title</th>
<th>Text</th>
<th colspan="3"></th>
<th>Total Number of Comments Per Article</th>
</tr>
<% #articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
<td><%= link_to 'Show', article_path(article) %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Destroy', article_path(article),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
<td>Placeholder</td>
</tr>
<% end %>
</table>
<p>Total Number of Articles in database: <%= #count_of_articles %></p>
<h4>All Comments Written So Far</h>
<% #comments.each do |c| %>
<p><b><%= c.commenter %></b></p>
<p><%= c.body %></p>
<% end %>
<%= link_to 'New Article', new_article_path %>
This is my Article's index action
def index
#articles = Article.all
#count_of_articles = #articles.count
#comments = Comment.all
end
I am trying to figure out how to place the number of comments that each article has in the <td>Placeholder</td>
How do I do that?
EDIT
class Comment < ActiveRecord::Base
belongs_to :article
end
class Article < ActiveRecord::Base
has_many :comments, dependent: :destroy
validates :title, presence: true, length: { minimum: 5 }
end
by doing article.comments.count in the view you're basically running a sql query for each article in your db. Why not gather all records in your controller, and ask for the count in your view?, something like:
in the controller:
#comments_by_article = Comment.all.group_by(&:article_id)
and, in your view:
<td><%= #comments_by_article[article.id] && #comments_by_article[article.id].count || 0 %></td>
that way, rails will cache #comments_by_article in your controller, and you'll hitting the database once.
It is simply count:
<table>
<tr>
<th>Title</th>
<th>Text</th>
<th colspan="3"></th>
<th>Total Number of Comments Per Article</th>
</tr>
<% #articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
<td><%= link_to 'Show', article_path(article) %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Destroy', article_path(article),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
<td><%= article.comments.count %></td>
</tr>
<% end %>
</table>
article.comments is an ActiveRecord::Relation which extends the ActiveRecord::Calculations class containing the count method. The relation was defined in your model by you. Internally, ActiveRecord will call a COUNT SQL query to determine the number of comments that matches your article.
You can get the count with
article.comments.count

What is error with cancan in view

I am using cancan to authorize the user in controller,And in views i am using Can?to check if the user is authorize to see that function
index.html.erb
<p id="notice"><%= notice %></p>
<h1>Listing Noticeboards</h1>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>society</th>
<th>Notice heading</th>
<th>Notice text</th>
<th>Active</th>
<th>Isdelete</th>
<th>Expire</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #noticeboards = #noticeboards.where(Isdelete: 0).find_each %>
<% #noticeboards.each do |noticeboard| %>
<tr>
<td><%= Society.find_by(id: noticeboard.id_society, Isdelete: 0).name %></td>
<td><%= noticeboard.notice_heading %></td>
<td><%= noticeboard.notice_text %></td>
<td><%= noticeboard.active %></td>
<td><%= noticeboard.IsDelete %></td>
<td><%= noticeboard.Expire %></td>
<td><%= link_to 'Show', noticeboard %></td>
<% if can? :update, noticeboard %>
<td><%= link_to 'Edit', edit_noticeboard_path(noticeboard) %> <% end %></td>
<% if can? :delete , noticeboard %>
<td>
<%= link_to 'Destroy', noticeboard, method: :delete, data: { confirm: 'Are you sure?' } %></td> <% end %>
</tr>
<% end %>
</tbody>
</table>
<br>
<% if can? :create, #noticeboard %>
<%= link_to 'New Noticeboard', new_noticeboard_path %>
<% end %>
In this if i change :delete to anything else, say :Hello, its still checking can, and if i am not using anything, it gives error saying "wrong number of arguments" what is the error, why its not taking :delete function?

Rails nested Resources Error

I have this nested recourses
resources :products do
resources :senders
end
In my products/index view I have this
...
..
.
<td><%= link_to 'Show Email Addresses', product_senders_path(product) %> </td>
.
..
...
which seemed to be working and it redirected me to the senders of that product. Now for some strange reason I get this:
NameError in Senders#index
undefined local variable or method `sender_path' for #<#<Class:0x00000003cb1f58>:0x00000003b46e48>
Extracted source (around line #18):
15: <td><%= sender.product_id %></td>
16: <td><%= sender.name %></td>
17: <td><%= sender.email %></td>
18: <td><%= link_to 'Show', sender %></td>
19: <td><%= link_to 'Edit', edit_sender_path(sender) %></td>
20: <td><%= link_to 'Destroy', sender, confirm: 'Are you sure?', method: :delete %></td>
21: </tr>
This is my sender/index file:
<h1>Listing senders</h1>
<table>
<tr>
<th>Application</th>
<th>Name</th>
<th>Email</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #senders.each do |sender| %>
<tr>
<td><%= sender.product_id %></td>
<td><%= sender.name %></td>
<td><%= sender.email %></td>
<td><%= link_to 'Show', sender %></td>
<td><%= link_to 'Edit', edit_sender_path(sender) %></td>
<td><%= link_to 'Destroy', sender, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>
<br />
Why I am getting this error? Before it was working fine
As there are only nested routes for senders, there are no edit_sender_path(sender) helper, only edit_product_sender_path(product, sender)
You may see a list of all application route helpers by executing rake routes

Resources