Rails 3:After_save 创建配置文件 [英] Rails 3: After_save create profile

查看:43
本文介绍了Rails 3:After_save 创建配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定在一起的用户模型和一个配置文件模型.

I have a user model and a profile model that are tied together.

我想做的是在用户每次注册时创建一个用户个人资料.

What I'd like to do is to create an user profile every time an user registers.

这是我现在在用户模型中的内容:

Here's what I have in the User model now:

 after_save :create_profile

  protected

  def create_profile
    @profile = current_user.build_profile(params[:profile])
    @profile.save
  end

然而,它似乎不起作用,因为 current_user 未被识别,这是有道理的,但是,有什么方法可以解决这个问题?

However, it doesn't seem to work because current_user is not recognized, which makes sense, however, what's the way to fix this?

谢谢

推荐答案

self.profile.create(params[:profile])

这篇关于Rails 3:After_save 创建配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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