表单中的第一个参数不能包含 nil 或为空 - Rails 4 [英] First argument in form cannot contain nil or be empty - Rails 4

查看:93
本文介绍了表单中的第一个参数不能包含 nil 或为空 - Rails 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 rails 中的联系表单收到此错误:

I receive this error with my contact form in rails:

First argument in form cannot contain nil or be empty

查看:

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

控制器

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?

推荐答案

错误消息告诉您不能有以下内容:

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

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

我的猜测是您的 @contact 设置为 nil 并且它不是来自您的 Contact#new 操作.

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| %>

虽然不推荐.

您需要检查包含您的表单的视图实际上是由您的 ContactsControllernew 操作呈现的.

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

这篇关于表单中的第一个参数不能包含 nil 或为空 - Rails 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆