如何调试未定义的方法form_with错误

时间:2017-09-21 17:33:04

标签: ruby-on-rails

我收到错误:

undefined form_with for #<#<Class:0x7ac62e0>:0x551e5a8> in articles#new

以下文件中出现错误:new.html.erb 哪个有:

<%= form_with(model: [@article] , local: true) do |f| %>
  <p>
    <%= f.label :title %><enter code here`br>
    <%= f.text_field :title %>
  </p>

控制器articles_controller.rb具有:

class ArticlesController < ApplicationController
  def new
  end
end

1 个答案:

答案 0 :(得分:2)

您的控制器只有新命令,请参阅下面的示例,了解创建实例变量@article

articles_controller.rb

class ArticlesController < ApplicationController 
  def new
    @article = Article.new
  end
end

new.html.erb

<%= form_with @article do |f| %>
  <%= f.label "Enter title: " %> 
  <%= f.text_field :title %> 
<% end %>