表单中的第一个参数不能包含 nil 或为空 - 用户注册表单 - rails 4 [英] First argument in form cannot contain nil or be empty - user sign up form - rails 4

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

问题描述

我正在尝试在 Rails 4 中创建一个基本的登录/注册表单,但我不断收到错误消息表单中的第一个参数不能包含 nil 或为空"@user.它不喜欢的部分是这样的:

I'm trying to create a basic login/sign up form in Rails 4 and I keep getting the error "first argument in form cannot contain nil or be empty" for @user. The part it doesn't like is this:

<%= form_for @user do |f| %>

但我的控制器中有新"方法,这是大多数询问此错误的人似乎遇到的问题:

but I have the 'new' method in my controller, which is the problem most people seem to have who ask about this error:

class UsersController < ApplicationController
  def new
    @user = User.new
  end

有人可以帮忙吗?

附录:将其更改为 <%= form_for User.new do 也不起作用...

addendum: changing it to <%= form_for User.new do doesn't work either...

推荐答案

你的表单正在寻找一个类的实例,在这种情况下用户是为了用它做一些事情(即创建一个新的或编辑一个现有的).为了允许用户在您的索引页面上注册(我不建议这样做 - 这会破坏 RESTful 设计),您还需要在 UsersController

Your form is looking for an instance of a class, in this case User in order to do something with it (i.e. create a new one or edit an existing one). In order for you allow users to register on your index page (which I wouldn't recommend - that breaks RESTful design) you would need to also have this in your UsersController

class UsersController < ApplicationController
  def new
    @user = User.new
  end

  def index
    @user = User.new
  end

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

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