第一次问一个问题,不确定我是否做得对,但是这里有。我收到此错误
表单中的第一个参数不能包含nil或为空 /nameofapp/app/views/products/_form.html.erb第1行引出:
以下是我的链接:
查看
<%= form_for(@product) do |f| %>
<% if @product.errors.any? %>
<div id="error_explanation">
控制器
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
# GET /products
# GET /products.json
def index
@products = Product.all
end
# GET /products/1
# GET /products/1.json
def show
end
# GET /products/new
def new
@product = Product.new
end
# GET /products/1/edit
def edit
end
# POST /products
# POST /products.json
def create
@product = Product.new(product_params)
respond_to do |format|
if @product.save
format.html { redirect_to @product, notice: 'Product was successfully created.' }
format.json { render :show, status: :created, location: @product }
else
format.html { render :new }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /products/1
# PATCH/PUT /products/1.json
def update
respond_to do |format|
if @product.update(product_params)
format.html { redirect_to @product, notice: 'Product was successfully updated.' }
format.json { render :show, status: :ok, location: @product }
else
format.html { render :edit }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
# DELETE /products/1
# DELETE /products/1.json
def destroy
@product.destroy
respond_to do |format|
format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_product
@product = Product.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def product_params
params.require(:product).permit(:name, :description, :image_url, :colour, :price)
end
端 #PATCH / PUT / products / 1 #PATCH / PUT /products/1.json def更新 respond_to do | format | if @ product.update(product_params) format.html {redirect_to @product,注意:&#39;产品已成功更新。&#39; } format.json {render:show,status :: ok,location:@product} 其他 format.html {render:edit} format.json {render json:@ product.errors,status :: unprocessable_entity} 结束 结束 端
# DELETE /products/1
# DELETE /products/1.json
def destroy
@product.destroy
respond_to do |format|
format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_product
@product = Product.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def product_params
params.require(:product).permit(:name, :description, :image_url, :colour, :price)
end
端
答案 0 :(得分:2)
我认为问题来自edit
行动,它没有@product
价值
它应该像这样更新
def edit
@product = Product.find(param[:id])
end
答案 1 :(得分:0)
@product为nil,你必须加载它。
您必须在所需的操作中将其加载到控制器中。就像编辑或显示动作一样。
@product = Product.find(params[:id])
或者在set_product中确保你拥有它。
private
def set_product
@product = Product.find(params[:id])
end
答案 2 :(得分:0)
如果我是正确的,您将在index.html.erb
中呈现部分内容,如果是,则将控制器中的index
操作更改为
def index
@products = Product.all
@product = Product.new
end