2015-08-28 37 views
2

第一次問一個問題並不確定我是否正確地做了這件事,但是在這裏。我收到此錯誤獲取「表單中的第一個參數不能包含零或爲空」錯誤

在形式上不能包含零或爲空 /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 /產品/ 1 #PATCH/PUT /products/1.json 高清更新 的respond_to做|格式| 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} 最終 結束 結束

# 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 

+1

你做錯了。請在問題中包含相關的代碼和錯誤。 –

+0

是更好嗎? – Frank

+0

@Frank請包括完整的錯誤。通常它指向一行代碼或對象/方法來查看。 – onebree

回答

0

如果我是正確的你渲染局部的index.html.erb,如果是更改控制器的index行動,以低於

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

修復它,但現在我的產品和「創造一個產品」重複他們自我25倍下頁 – Frank

+0

@Frank不明白你在說什麼..但你應該發表另一個問題,解釋什麼是問題... – Pavan

+0

好吧,會感謝! – Frank

2

我覺得出具來自edit行動,它不具備@product

它應該被更新爲這樣的東西

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

仍然是一樣的錯誤。 – Frank

+0

這不是問題。控制器有'set_product'來完成這項工作。 – Pavan

+0

如果'@ product'的'set_product'設置了值,那麼@product已經更改爲nil,請檢查'edit.html。erb'以確保它不會發生 –

0

@product是零,你必須加載它。

您必須在控制器中將其加載到所需的操作中。就像在編輯或顯示操作中一樣。

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

或確保在set_product中擁有它。

private 

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

是的,它已經在set_product – Frank

+0

寫在問題上,然後請 – AlexLarra

相關問題