Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
367 views
in Technique[技术] by (71.8m points)

First argument in form cannot contain nil or be empty - Rails 4

I receive this error with my contact form in rails:

First argument in form cannot contain nil or be empty

View:

     <%= form_for @contact do |f| %>
     and so on.......

Controller

def new
    @contact = Contact.new
end
 and so on....

I found related errors, but none of the solutions seems to fit my particular issue. Any clues to what could be causing this?

question from:https://stackoverflow.com/questions/18853721/first-argument-in-form-cannot-contain-nil-or-be-empty-rails-4

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The error message is telling you that you can't have the following:

<%= form_for nil do |f| %>
<%= form_for [] do |f| %>

My guess here is that your @contact is set to nil and that it doesn't come from your Contact#new action.

FYI it would simply work if you do this:

<%= form_for Contact.new do |f| %>

Though it is not recommended.

You need to check that the view containing your form is actually rendered by the new action of your ContactsController.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...