表单中的第一个参数不能包含nil或者在Productcategories中为空ArgumentError#new

时间:2017-04-17 09:54:02

标签: ruby-on-rails

我收到此错误:

  

ActionView :: Template :: Error(表单中的第一个参数不能包含nil   或者是空的):        7:新增商品分类        8:        9:       10:<%= form_for @productCategories do | f | %GT; < - 错误在这里 - >       11:

      12:<%= f.label:商品分类名称:%>       13:<%= f.text_field:name%>应用程序/视图/ productcategories / new.html.erb:10:在   `_app_views_productcategories_new_html_erb__593308190794838703_70267444710660'   渲染   /Users/liupengfei/.rvm/gems/ruby-2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb   在救援/布局内渲染   /Users/liupengfei/.rvm/gems/ruby-2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_source.html.erb   渲染   /Users/liupengfei/.rvm/gems/ruby-2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_source.html.erb   (4.1ms)渲染   /Users/liupengfei/.rvm/gems/ruby-2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb呈现   /Users/liupengfei/.rvm/gems/ruby-2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb(2.1ms)渲染   /Users/liupengfei/.rvm/gems/ruby-2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb

这是我的源代码:

def new
  @productCategory = ProductCategory.new
end

def create
  @productCategory = ProductCategory.new(ProductCategory_params)

  if @ProductCategory.save
    redirect_to productcategories_path
  else
    render :new
  end
end

请帮我解决问题。

1 个答案:

答案 0 :(得分:0)

您使用

形式的错误实例变量名称
def new
  @productCategory = ProductCategory.new
end

更改

<%= form_for @productCategories do |f| %> <- 错误在这里 ->

<%= form_for @productCategory do |f| %> <- 错误在这里 ->

修改

对于您遇到的错误,您需要手动传递路径

<%= form_for @productCategory, url: productcategories_path do |f| %> <- 错误在这里 ->