使用Devise注册时,如何将值设置为嵌套列? [英] How can I set the value to the nested column when signing up with Devise?

查看:148
本文介绍了使用Devise注册时,如何将值设置为嵌套列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户模型有一个user_profile。

我刚刚安装了devise,它的工作正常。

User model has one user_profile.
I just installed devise and its working fine.

我做了registrations_controller.rb,它已经创建','after_update_path_for(资源)
'和'编辑'动作。

I made registrations_controller.rb and it has 'create', 'after_update_path_for(resource) ', and 'edit' actions.

如果我想输入'45'到嵌套的user_profile列默认值,我如何在注册控制器中进行代码?

If I want to make it input '45' to nested column of user_profile as default value, how can I code in registration controller???

我应该做另一个动作叫做save / or new?,然后写这个?

Should I make another action called 'save/or new?' , and write this?


@ user.user_profile.language_id ='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)

I was making some tests, and I suggest you to use callbacks, but you can also override the actions of the RegistrationsController of Devise. I followed this thread: Override devise registrations controller and the code of the controller https://github.com/plataformatec/devise/blob/master/app/controllers/devise/registrations_controller.rb (see that I use sign_in instead of sign_up - I'm working with Rails 3.2.8)

这是代码与编辑的情况:

And this is the code with the edits for the case:

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 

正如帖子的答案所说,我离开路线如下:

And as the answer of the post said, I leave the routes as follows:

devise_for :users, :controllers => {:registrations => "registrations"}

我使用了一个 has_one 关系。

这篇关于使用Devise注册时,如何将值设置为嵌套列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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