【发布时间】:2015-11-23 10:03:53
【问题描述】:
第一次问一个不确定我是否做得对的问题,但这里是。我收到此错误
表单中的第一个参数不能包含 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
结束 # 补丁/放置 /products/1 # PATCH/PUT /products/1.json 定义更新 respond_to 做 |格式| 如果@product.update(product_params) format.html { redirect_to @product, notice: '产品更新成功。' } format.json { 渲染:显示,状态::好的,位置:@product } 别的 format.html { 渲染:编辑 } format.json { 渲染 json:@product.errors,状态::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
结束
【问题讨论】:
-
你做错了。请在问题中包含相关代码和错误。
-
@Frank 请包含完整的错误。通常它指向一行要查看的代码或对象/方法。
-
能否把问题中的相关控制器代码贴出来?
标签: ruby-on-rails