0

Link to image of what I'm talking about

My problem is that there is an extra template being rendered at the bottom of my list without an item id. The icons delete the item they are next to when clicked. The bottom icon is showing up even though it has no item related to it.

Here is my the show html.erb

<div class="row">
<div class="col-md-8">
    <div class="media">
        <br />
        <div class="media-body">
            <h3><%= @user.email %></h3>
            <br />
            <%= render partial: 'items/form', locals: {user: @user} %>
            <div class="media">
                <div class="media-body">
                    <h4 class="media-heading">
                        <ul>
                            <%= render @items %>
                        </ul>    
                    </h4>
                </div>
            </div>
        </div>
    </div>
</div>

Here is my _item template

<li><%= item.name %><%= link_to "", [current_user, item], remote: true, method: :delete, class: 'glyphicon glyphicon-ok' %></li>

Here is my items controller

class ItemsController < ApplicationController
def create
  @item = Item.new(item_params)
  @item.user = current_user

  if @item.save
    flash[:notice] = "Item was saved."
    redirect_to user_path(current_user.id)
  else
    flash.now[:alert] = "There was an error saving the item. Try again."
    redirect_to user_path(current_user.id)
  end
end

def destroy
  @item = Item.find(params[:id])

  if @item.destroy
    flash[:notice] = "Item deleted"
    redirect_to user_path(current_user.id)
  else
    flash.now[:alert] = "Error deleting the item."
    redirect_to user_path(current_user.id)
  end
end

private
def item_params
  params.require(:item).permit(:name)
end

end

Here is my users controller

class UsersController < ApplicationController

  def show
    @user = current_user
    @item = Item.new
    @items = @user.items
  end
end

The items belong to the users and have nested routes under user. The last checkmark (the one I want to get rid of) when hovered over shows the link .../users/6/items, while the others that next to items show .../users/6/items/40

Here is my _form.html.erb for items:

<%= form_for [user, user.items.new] do |f| %>
  <%= f.text_field :name, class: 'form-control', placeholder: "Enter your to-do item", value: nil, required: true %>
  <div class="form-group">
    <%= f.submit "Save", class: 'btn btn-success' %>
  </div>
<% end %>
Nich B
  • 1
  • 1

1 Answers1

0

Remove

@item = Item.new

from show action in users controller.

class UsersController < ApplicationController

  def show
    @user = current_user
    @items = @user.items
  end
end
Ritesh Ranjan
  • 1,012
  • 9
  • 16
  • I removed @item from show in the users controller, but the extra icon at the bottom is still there. – Nich B Dec 22 '17 at 21:28
  • Please post your 'items/form' – Ritesh Ranjan Dec 23 '17 at 02:55
  • I just added it at the bottom of the original question. Also, I found a temporary solution that is working, by adding this loop before
  • : <% @user.items.slice(0, @user.items.count).each do |item| %>
  • – Nich B Dec 23 '17 at 21:24