2015-09-12 17 views
0

嗨我遇到了問題:「表單中的第一個參數不能包含零或爲空」。由於我是Rails的新受了我很難用現有的答案(仍然無法找到我的錯誤)請看看:RAILS 4表單中的第一個參數不能包含零或爲空

Showing /home/ubuntu/workspace/app/views/admin/categories/_form.html.haml where line #1 raised: 

First argument in form cannot contain nil or be empty 
Extracted source (around line #1): 


= simple_form_for [:admin, @category], :html => {class: "form-horizontal"} do |f| 
    .form-group 
    = f.label :name, class: "col-sm-2 control-label" 
    .col-sm-10 
     = f.input_field :name, label: false, class: "form-control" 
    - if @category.id.present? 

渲染:

= render 'form', :url => admin_categories_path(@category) 

控制器:

class Admin::CategoriesController < AdminController 

before_action :set_category, :only =>[:show, :edit, :destroy, :update] 

    def index 
    @categories = Category.all 
    respond_to do |format| 
     format.html 
     format.json {render json: @categories} 
    end 
    end 

    def show 
    respond_to do |format| 
     format.html 
     format.json { render json: @category } 
    end  
    end 

    def new 
    @category = Category.new 
    end 

    def create 
    @category = Category.new(category_params) 

    respond_to do |format| 
     if @category.save 
      format.html {redirect_to admin_categories_path, notice: 'Category created'} 
     else 
      format.html {render action: 'new', alert: 'Error while creating'} 
     end 
    end  
    end 

    def edit 
    end 

    def update 

    respond_to do |format| 
     if @category.update{post_params} 
      format.html {redirect_to admin_post_path, notice: "Category updated"} 
     else 
      format.html {render action: 'edit', alert: "Error while editing"} 
     end 
    end 

    end 

    def destroy 
    @category.destroy 
    respond_to do |format| 
     format.html {redirect_to admin_categories_path, notice "#{Category.title} deleted"} 
    end 
    end 

    protected 

    def get_category 
    @category = Category.find(params[:id]) 
    end 

    def category_params 
    params.require(:category).permit(:title, :body) 
    end 
end 
+0

出現這樣的情況,當你正在尋找新的或編輯行動? –

+0

它發生在我去的路線: /管理員/類別/新 所以後 – Hoggie

+0

錯誤是說'形式的第一個參數不能包含零或爲空提取源(繞行#1):'所以無論' category'爲零,或者將':admin'傳遞給'simple_form_for [:admin,@category]'正在評估爲零 –

回答

0

在您的before_action中,確保您在陣列中有:new。現在你沒有,所以@category沒有被設置,當你去/管理/類別/新

+0

它正在被'new'操作設置。 –

+0

你的意思是在控制器中? 如果是,它沒有幫助 – Hoggie

0

好吧,我明白了。 得到一點就可以了新的觀點:

所以 - 它呈現類控制器admin目錄 - 這是空

相關問題