如何将Profile Controller与用户控制器连接?

时间:2012-12-29 12:41:31

标签: ruby-on-rails ruby-on-rails-3

我有通过Devise的USER并通过脚手架创建了Profile,现在一切正常但我无法将用户“连接”到配置文件。

这是我到目前为止所做的 -

user.rb

has_one :profile

profile.rb

belongs_to :user

我也通过配置文件表中的迁移创建了user_id列。

现在,当我登录并填写表单/ profiles / new时,它会创建配置文件,但由于user_id字段为NULL,因此无法链接到用户。此外,用户可以创建多个配置文件,因为我认为他只能创建一个,如我所说:has_one关系?

任何帮助?

编辑 - 在profiles_controller文件中,我也尝试更改

@user_profile = UserProfile.new(params[:user_profile])

@user_profile = current_user.userprofile.new(params[:user_profile])

但它提供了未定义的方法'UserProfile'

1 个答案:

答案 0 :(得分:2)

@user_profile = current_user.userprofile.new(params[:user_profile])

应该是

@user_profile = current_user.build_user_profile(params[:user_profile])