我正在尝试为产品创建添加一个基本表单。我能够创建新产品并展示产品,但是当我尝试编辑产品时,我面临“NilClass:Class 的未定义方法‘model_name’”错误
这是我的 PrdouctControllerClass: (controllers/products_controller.rb)
class ProductsController < ApplicationController
def index
@products = Product.order("name")
@products =Product.new
@products =Product.all
end
def show
@product = Product.find(params[:id])
end
def new
@product = Product.new
end
def create
@product = Product.new(product_params)
if @product.save
redirect_to @product, notice: "Successfully created product."
else
render :new
end
end
private
def product_params
params.require(:product).permit(:name, :price, :category, :ratings)
end
end
def edit
@product = Product.find (params[:id])
end
private
def id_params
params.require(:product).permit(:name, :price, :ratings, :category)
end
def update
@product = Product.find(params[:product])
redirect_to @product, notice: " Successfully updated product"
else
render :edit
end
def destroy
@products = Product.find (params[:id])
@product.destroy
redirect_to products_url, notice: "successfully destroyed products"
end
**and here is the form (App/Views/products/_form.html.erb**)
<%= simple_form_for(@product) do |f| %>
<% if @product.errors.any?%>
<div class="error_messages">
<h2><%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:</h2>
<ul>
<% @product.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :price %><br />
<%= f.text_field :price %>
</div>
<div class="field">
<%= f.label :released_on %><br />
<%= f.date_select :released_on %>
</div>
<div class="field">
<%= f.check_box :discontinued %>
<%= f.label :discontinued %>
</div>
<div class="field">
<%= f.label :rating %><br />
<%= f.radio_button :rating, 1 %> 1
<%= f.radio_button :rating, 2 %> 2
<%= f.radio_button :rating, 3 %> 3
<%= f.radio_button :rating, 4 %> 4
<%= f.radio_button :rating, 5 %> 5
</div>
<div class="field">
<%= hidden_field_tag "product[category_ids][]", nil %>
<% Category.all.each do |category| %>
<%= check_box_tag "product[category_ids][]", category.id, @product.category_ids.include?(category.id), id: dom_id(category) %>
<%= label_tag dom_id(category), category.name %><br />
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
这是我收到的错误消息:_app_views_products__form_html_erb__1297814100543601928_70242432121140 app/views/products/_form.html.erb,第 1 行未定义的方法 `model_name' for NilClass:Class 我试图搜索 Google/Stafkoverflow 但我没有找到任何相关的问题到这个问题。提前感谢您的帮助