1

我是一名 Rails 新手,我已经开始编写一个网络应用程序。

我使用设计来设置用户注册并生成设计视图模板。

我在那里添加了自定义模型 - 用户名、名字、姓氏等。然后我将它们添加到 user.rb 等的 attr_accessor 中,并验证这些细节的存在

我想在成功工作的编辑注册表单中添加这些元素。

在注册页面上 - 代码仅要求输入电子邮件、密码、确认密码(设计为默认设置)。

如果我现在尝试注册为新用户(在所有这些步骤之后),我会收到一条错误消息,指出名字不能为空,姓氏不能为空等。

我如何从注册中排除这些,但在用户配置文件编辑中保持它们处于活动状态?

我希望我说得通。感谢您的提前帮助

4

2 回答 2

0

您可以进入您的视图 > 设计文件夹并创建一个注册文件夹(如果它不存在)并创建一个 new.html.erb 看看您在链接下找到的文件: https ://github.com/plataformatec/devise /blob/master/app/views/devise/registrations/new.html.erb

将其复制到您的新文件并根据需要对其进行自定义...它应该覆盖设计默认视图。

于 2013-01-06T02:49:47.310 回答
0

If I understand correctly, during signup/registration you want to only ask for email and password, excluding the other User model attributes (first name, surname) from that form. However you also want to later have these other attributes validated when the user edits their profile.

So since you are validating for the presence of these extra attributes which are not provided when the signup form is submitted, the attempt to create a new user record simply fails to create at validation.

Try the :on => :update validation option to specify that certain fields should only be validated when later updated, rather than the default which is to validate any time a record is saved. Like this:

class User < ActiveRecord::Base
    validates :email, :presence => true
    validates :firstname, :presence => true, :on => :update
    validates :surname, :presence => true, :on => :update
    ...
end

See http://guides.rubyonrails.org/active_record_validations_callbacks.html#on

于 2013-01-06T10:02:03.423 回答