【问题标题】:Getting "First argument in form cannot contain nil or be empty" error获取“表单中的第一个参数不能包含 nil 或为空”错误
【发布时间】: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


【解决方案1】:

我认为问题来自edit 操作,它没有@product

它应该像这样更新

def edit
  @product = Product.find(param[:id])
end

【讨论】:

  • 这不是问题。控制器有 set_product 来完成这项工作。
  • 如果set_product@product 设置了值,那么可能@product 已在某处更改为nil,请检查edit.html.erb 以确保它不会发生
【解决方案2】:

如果我是正确的,您正在渲染 index.html.erb 中的部分,如果是这样,请将控制器中的 index 操作更改为下面

def index
  @products = Product.all
  @product = Product.new
end

【讨论】:

  • 修复了它,但现在我的产品和“创建产品”在页面下方重复了 25 次
  • @Frank 听不懂你在说什么......但你应该发布另一个问题来解释问题所在......
【解决方案3】:

@product 为 nil,您必须加载它。

你必须在你想要的动作中将它加载到控制器中。就像在编辑或显示操作中一样。

@product = Product.find(params[:id])

或者确保在 set_product 你有它。

private

def set_product
  @product = Product.find(params[:id])
end

【讨论】:

  • 请写在问题中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多