gpt4 book ai didi

ruby - 无法编辑项目错误NoMethodError in Items#edit

转载 作者:数据小太阳 更新时间:2023-10-29 08:03:36 24 4
gpt4 key购买 nike

我有一个元素模型。它适用于零售店。

当我在项目 View 上时。让我们说项目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>

下面是我的编辑按钮在项目 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>

这是我的 Items 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 %>

最佳答案

您没有在edit 操作中定义@user

可能在您为请求 @user.errors.any?edit 操作呈现的模板中的某处。禁用此部分或修改为类似 @item.errors.any?

关于ruby - 无法编辑项目错误NoMethodError in Items#edit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31137104/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com