Cannot edit Error NoMethodError element in # edit elements

I have a product model. Its for a retail store.

When im on item view. Let's say for the element id 11. Then I click the edit button. I am getting the following error

Anyone have any hints? Any help would be greatly appreciated.

NoMethodError in Items#edit
undefined method `errors' for nil:NilClas
<% if @user.errors.any? %>
  <div id="error_explanation">
    <div class="alert alert-danger">
      The form contains <%= pluralize(@user.errors.count, "error") %>.
    </div>
    <ul>

      

Below is my edit button in View

<!-- right column -->
  <div class="col-lg-6 col-md-6 col-sm-5">
      <div class = "itemtitle"><%= @item.title %> </div> 
      <div class = "price">$<%= @item.price %> </div> 
      <%= link_to "Edit Items", edit_item_path(@item) %>
      <div class="productFilter productFilterLook2">
          <div class="filterBox">
              <select>
                  <option value="strawberries" selected>Quantity</option>
              </select>
          </div>

      

Here is my item controller.

class ItemsController < ApplicationController

  def index
    @items = Item.paginate(page: params[:page])
  end

  def new
    @item = Item.new
  end

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

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

  def update
    if @item.update(pin_params)
      redirect_to @item, notice: 'Item was successfully updated.'
    else
      render action: 'edit'
    end
  end

  def create
    @item = current_user.items.build(item_params)
    if @item.save
      redirect_to @item
      flash[:success] = "You have created a new item"
    else
      flash[:danger] = "Your item didn't save"
      render "new"
    end
  end

  def destroy
    @item.destroy
    respond_to do |format|
      format.html { redirect_to items_url, notice: 'Item was successfully deleted' }
      format.json { head :no_content }
    end
  end

  private

    def item_params
      params.require(:item).permit(:title, :price, :description, :image)
    end

end

      

_error_messages.html.erb

<% if @user.errors.any? %>
  <div id="error_explanation">
    <div class="alert alert-danger">
      The form contains <%= pluralize(@user.errors.count, "error") %>.
    </div>
    <ul>
    <% @user.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
    </ul>
  </div>
<% end %>

      

+3


source to share


1 answer


You do not define @user

in your action edit

.



Perhaps somewhere in the template that you are performing for the action edit

you are requesting for @user.errors.any?

. Either disable this part or change it to @item.errors.any?

etc.

+2


source







All Articles