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 %>
Related
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>
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!
thanks in advance for your help!
I would like to concatenate 2 variables and display both in one cell in table in the view. More specifically I want to display column "game score" e.g. 5:7 by concatenating #game.team1_score and #game.team2_score.
Can you help out with this one line?
<% #games.each do |game| %><tbody>
<tr>
<td><%= game.id %> </td>
<td><%= game.team1 %></td>
<td><%= game.team2 %></td>
<td><%= game.team1_score ":" game.team2_score %></td>
<td><%= game.updated_at %></td>
<td><%=link_to("Detail", game_path(game.id), class: 'action show') %></td>
<td><%=link_to("Edit", edit_game_path(game.id), class: 'action edit') %></td>
<td><%=link_to("Delete", game_path(game.id), method: :delete, class: 'action delete') %> <br></td>
<% end %>
</tr>
try this!
<td><%= game.team1_score %>:<%= game.team2_score %></td>
The following code is implemented to read in values and fill in a table with the appropriate values, however when one of the methods doesn't return anything it produces an error and crashes the page.
<tr class="event-row" >
<td><%= event.description %></td>
<td><%= event.contact.name %></td>
<td><%= event.start.strftime('%H:%M') %></td>
<td><%= event.end.strftime('%H:%M') %></td>
<td><%= link_to "edit", edit_event_path(event) %></td>
<td><%= link_to "delete", event, method: :delete, data: {confirm: "Are you sure?"} %>
<td><%= link_to "show", event_path(event) %></td>
</tr>
How can you check to see if the methods are returning no values?
you can also check if that value is present with if
<tr class="event-row" >
<td><%= event.description %></td>
<td><%= event.contact.name if event.contact %></td>
<td><%= event.start.strftime('%H:%M') if event.start %></td>
<td><%= event.end.strftime('%H:%M') if event.end %></td>
<td><%= link_to "edit", edit_event_path(event) %></td>
<td><%= link_to "delete", event, method: :delete, data: {confirm: "Are you sure?"} %>
<td><%= link_to "show", event_path(event) %></td>
</tr>
You can use try
Try will returns nil rather than raising an exception
event.try(:description)
event.try(:contact).try(:name)
event.try(:start)
event.try(:end)
For more clarification, have a look at this
try public method
You can use try(:name) or delegate with allow_nil:
add this to your event model:
delegate :name, to: :contact, prefix: true, allow_nil: true
and this in your view:
<td><%= event.contact_name %></td>
I have a two way many-to-many assoication between 3 models: work.rb, category.rb, categorywork.rb
Within the work#index using <%= work.categories %> renders some wonky looking html markup
<% #works.each do |work| %>
<tr>
<td><%= work.name %></td>
<td><%= work.subtitle %></td>
<td><%= work.categories %></td>
<td><%= link_to 'Show', work %></td>
<td><%= link_to 'Edit', edit_work_path(work) %></td>
<td><%= link_to 'Destroy', work, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
I'm trying to target speific attributes of the association like "name".
Unfortunately when using <%= work.categories.name %> it gets weirder with:
How do i target just the name or just the description?
Try this out:
<%= work.categories.pluck(:name) %>