无法在项目编辑中编辑项目错误NoMethodError

时间:2015-06-30 11:31:50

标签: ruby ruby-on-rails-3 ruby-on-rails-4

我有一个物品模型。它适用于零售店。

当我在项目视图上时。让我们说项目ID 11.然后我点击编辑按钮。我收到以下错误

任何人都有线索吗?任何帮助将不胜感激。

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>

下面是我的编辑按钮在项目视图中的位置

<!-- 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>

这是我的物品控制器。

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 %>

1 个答案:

答案 0 :(得分:2)

您未在@user操作中定义edit

您可能会在模板的某个位置为edit要求的@user.errors.any?操作进行呈现。禁用此部分或修改为@item.errors.any?等等。