Rails Error comparing object with parameter inside view - ruby-on-rails

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!

Related

Passing a param with a delete button in Rails

I have a button that I want to increment a cart quantity down by 1.
My controller action finds the param (quantity) empty.
How do I make this work please?
I have looked at many SO articles on here to try and find a solution!
<% #cart.line_items.each do |item| %>
<tr>
<td><%= button_to 'Remove item from cart', item, method: :delete, data: {confirm: 'Are you sure?' } %></td>
##### PROBLEM LINE BELOW
<td><%= button_to ' - 1 ', item, :quantity => 1, method: :delete %></td>
<td><%= item.quantity %></td>
<td><%= button_to ' + 1 ', line_items_path(product_id: item.product_id) %></td>
<td><%= item.product.title %></td>
<td class="item_price"><%= number_to_currency(item.total_price) %></td>
</tr>
<% end %>

Error when delete comment 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>

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 index list a variable does change during looping

I'm trying to set a button for each Client in an index list that will take you to that Client's Location.
This is my code:
<% Client.active.find_each do |client| %>
<tr>
<td><strong><%= link_to client.client_name, client_path(client) %></strong></td>
<td><%= client.locname %></td>
<td><%= client.phone1 %></td>
<td><%= client.fax %></td>
<td><%= client.worequests.notcompl.count %></td>
<td><%= client.workorders.count %></td>
<td><%= client.contacts.count %></td>
<% location = Location.where('locname' == client.locname).first.id %>
<td><%= link_to 'Tree', location_url(location), :class => 'btn btn-mini btn-primary' %></td>
<% if current_user.has_role? :admin or current_user.has_role? :super %>
<td><%= link_to 'Edit', edit_client_path(client), :class => 'btn btn-mini btn-success' %></td>
<% else %>
<td></td>
<% end %>
</tr>
<% end %>
But, the link is always to location/13 - which is from the first location.where lookup.
Why doesn't each Client's Tree button point to their location?
Thanks for your help!
I think this is your problem:
Location.where('locname' == client.locname)
should be
Location.where(:locname => client.locname)
Your where clause is evaluating to true no matter what. I think you want:
Location.where('locname=?', client.locname).first.id

Rails 3 task scopes?

I am working on a simple rails 3 todo application and I am trying to filter the app by completed tasks and none completed task but whenever I try calling a scope I get the error message.
undefined method `completed' for #<Array:0x007fe8420d0e58>
task.rb
scope :completed , where(:completed => true)
scope :incomplete , where(:finished => false)
index.html.erb
<table>
<tr>
<th>Name</th>
<th>Description</th>
<th>Finished</th>
<th>User</th>
</tr>
<% #tasks.each do |task| %>
<tr>
<td><%= task.name %><%= button_to "complete", complete_task_path(task.id)%></td>
<td><%= task.description %></td>
<td><%= task.finished %></td>
<td><%= task.user_id %></td>
<td><%= link_to 'Show', task %></td>
<td><%= link_to 'Edit', edit_task_path(task) %></td>
<td><%= link_to 'Destroy', task, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>
<%= content_tag :h2, "Stuff Ive done" %>
<table>
<tr>
<th>Name</th>
</tr>
<% #tasks.completed.each do |task| %>
<tr>
<td><%= task.name %></td>
</tr>
<% end %>
</table>
task_controller.rb
def complete
#task = Task.find(params[:task_id])
#task.completed = true
#task.save
redirect_to task_path
end
routes.rb
match "tasks/:id/complete" => "task#complete", :as => :complete_task
Any reasons why rails is giving me this error?
Just by looking at your view (index.html.erb), in one place, you are treating as a relation.
<% #tasks.each do |task| %>
Later in the code, you are trying to access it as a single object.
<% #tasks.completed.each do |task| %>
Since you are seeing the error on the second instance, you need to access 'completed' as in:
<% #tasks.completed.each do |task| %>
<% completed = task.completed %>
<% completed.each do |com| %>
Does this make sense?

Resources