虚拟属性与Rails中的Devise 4 [英] Virtual attributes with Devise in Rails 4

查看:116
本文介绍了虚拟属性与Rails中的Devise 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的用户模型没有一个:name 字段,但我想要包含一个:name 字段我的注册表单将用于在 after_create 中创建另一个模型的记录。

My User model doesn't have a :name field but I want to include a :name field in my sign up form which will be used to create a record of another model in an after_create.

class User < ActiveRecord::Base
  after_create :create_thing

private
  def create_thing
    @thing = Thing.new
    @thing.name = <here's where I need help>
    @thing.save!
  end
end

如何从注册表单中获取名称

How can I get the name from the sign up form?

推荐答案

在您的模型中,添加attr_accessor作为名称

In your model add attr_accessor for name

attr_accessor :name

这将允许您将其添加到表单中使用它在您的after_create

This will allow you to add it to a form and use it to generate the proper data in your after_create

这篇关于虚拟属性与Rails中的Devise 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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