Ruby on rails 在使用Deviate注册时,如何设置嵌套列的值?
用户模型有一个用户配置文件。Ruby on rails 在使用Deviate注册时,如何设置嵌套列的值?,ruby-on-rails,ruby-on-rails-3,devise,Ruby On Rails,Ruby On Rails 3,Devise,用户模型有一个用户配置文件。 我刚刚安装了这个装置,它工作正常 我注册了\u controller.rb,它有'create','after\u update\u path\u(资源) ,以及“编辑”操作 如果我想将“45”作为默认值输入到用户配置文件的嵌套列中,我如何在注册控制器中编码 我是否应该执行另一个名为“保存/或新建”的操作,并编写以下内容 @user.user\u profile.language\u id='45' 我正在做一些测试,我建议您使用回调,但您也可以覆盖Desive的R
我刚刚安装了这个装置,它工作正常 我注册了\u controller.rb,它有'create','after\u update\u path\u(资源) ,以及“编辑”操作 如果我想将“45”作为默认值输入到用户配置文件的嵌套列中,我如何在注册控制器中编码 我是否应该执行另一个名为“保存/或新建”的操作,并编写以下内容 @user.user\u profile.language\u id='45'
我正在做一些测试,我建议您使用回调,但您也可以覆盖Desive的RegistrationController的操作。我遵循了以下思路:以及控制器的代码(请参阅我使用的是登录而不是注册——我正在使用Rails 3.2.8) 这是代码和案例的编辑:
class RegistrationsController < Devise::RegistrationsController
def new
super
end
def create
build_resource
resource.build_user_profile
resource.user_profile.language_id = 45
if resource.save
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_navigational_format?
sign_in(resource_name, resource)
respond_with resource, :location => after_sign_up_path_for(resource)
else
set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
expire_session_data_after_sign_in!
respond_with resource, :location => after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords resource
respond_with resource
end
end
def update
super
end
end
我使用了一个has_one关系。我认为您需要覆盖,但您也可以使用callbacks@pablo89谢谢!那么你的意思是我应该在注册控制器中创建操作之前执行?但是只编写@user.user\u profile.language\u id='45'就可以了吗?我不知道Deave是如何保存的:(我还没有尝试过(在控制器中),但我会在几个小时后做一个测试哈哈,我建议你在创建之后(我正在考虑制作一个新对象,但可能有一个更短的方法,我会尝试…)谢谢!!实际上我想用before\u create方法作为回调。所以我做了before\u create并编码了build\u resource;resource.build\u user\u profile;resource.user\u profile.language\u id=45;inside。但是它不起作用:(创建嵌套记录时,它仍然不会更改值。)
devise_for :users, :controllers => {:registrations => "registrations"}