用户模型有一个 user_profile。
我刚刚安装了设计并且工作正常。
我制作了registrations_controller.rb,它有“create”、“after_update_path_for(resource)”和“edit”操作。
如果我想让它在 user_profile 的嵌套列中输入“45”作为默认值,我该如何在注册控制器中编码???
我应该进行另一个名为“保存/或新建”的操作吗?,然后写这个?
@user.user_profile.language_id = '45'
用户模型有一个 user_profile。
我刚刚安装了设计并且工作正常。
我制作了registrations_controller.rb,它有“create”、“after_update_path_for(resource)”和“edit”操作。
如果我想让它在 user_profile 的嵌套列中输入“45”作为默认值,我该如何在注册控制器中编码???
我应该进行另一个名为“保存/或新建”的操作吗?,然后写这个?
@user.user_profile.language_id = '45'
我正在做一些测试,我建议你使用回调,但你也可以覆盖 Devise 的 RegistrationsController 的操作。我关注了这个线程:覆盖设计注册控制器和控制器的代码https://github.com/plataformatec/devise/blob/master/app/controllers/devise/registrations_controller.rb(请参阅我使用sign_in而不是sign_up -我正在使用 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
正如帖子的答案所说,我留下的路线如下:
devise_for :users, :controllers => {:registrations => "registrations"}
我使用了has_one关系。