Lacomus
Lacomus

Reputation: 37

Rails form_for @form "First argument in form cannot contain nil or be empty"

I'm getting the error "First argument in form cannot contain nil or be empty" when I use form_for with a homepage form element. Not sure what I'm doing wrong.

home.html.erb

<%= form_for @form do |f| %>
  <div class="form-group">
    <%= f.text_field :userInput, placeholder: "Press 'Enter' to move on.", class: 'form-control' %>
  </div>
<% end %>

forms_controller.rb

class FormsController < ApplicationController
 def new
  @form = Form.new
 end
 def create
  @form = Form.new(form_params)
  @form.save!
 end
 private
  def form_params
   params.require(:form).permit(:userInput)
  end
end

EDIT: So as suggested I moved the code from FormsController to PagesController that was already rendering the home page, but the same error still persists. Here's PagesController:

class PagesController < ApplicationController
  def home
  end
  def new
    @form = Form.new
  end
  def create
    @form = Form.new(form_params)
    @form.save!
  end
  private
  def form_params
    params.require(:form).permit(:userInput)
  end
end

Upvotes: 0

Views: 57

Answers (1)

rynomite
rynomite

Reputation: 76

Is FormController the one rendering the home.erb.html view? The controller only has a new and create action, so i would assume the view should've been named new.erb.html or should've thrown an error saying the view isn't found.

In the case of getting this error, it seems another controller is rendering the view, in which that controller should have the @form = Form.new instance variable getting set.

Can you confirm the path of where home.erb.html lives?

Upvotes: 0

Related Questions