2
votes

First time asking a question not sure if I'm doing it right, but here goes. I am getting this error

First argument in form cannot contain nil or be empty /nameofapp/app/views/products/_form.html.erb where line #1 raised:

Here are my links:

view

<%= form_for(@product) do |f| %>
  <% if @product.errors.any? %>
    <div id="error_explanation">

controller

  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

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

end

3
You doing wrong. Please include the related code and errors in the question.Зелёный
@Frank please include the full error. Usually it points to a line of code or object/method to look at.onebree
Can you post the related controller code in the question?Pavan

3 Answers

0
votes

If I'm correct you are rendering the partial in index.html.erb, if so change the index action in the controller to below

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

I think issue come from edit action, it don't have @product value

it should be updated as something like this

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

@product is nil and you must load it.

You must load it in the controller in the action you want it. Like in edit or show actions.

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

Or ensure in set_product you have it.

private

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