0

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

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

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

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

@user.user_profile.language_id = '45'

HUSTEN
  • 5,117
  • 4
  • 24
  • 38
  • I think you will need to override, but you can also use callbacks http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html – pablomarti Dec 11 '12 at 05:46
  • @pablo89 thanks! so you mean that I should make before_create action in registration controller? But is that okay to just code @user.user_profile.language_id='45'? I don't know how devise does save:( – HUSTEN Dec 11 '12 at 06:04
  • I haven't tried that (in the controller), but I will make a test in a few hours haha, and I suggest you after_create (I was thinking making a new object, but there might be a shorter way, which I will try...) – pablomarti Dec 11 '12 at 06:10

1 Answers1

1

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"}

I used a has_one relationship.

Community
  • 1
  • 1
pablomarti
  • 2,087
  • 2
  • 22
  • 35
  • Thanks!! actually I wanted to use before_create method as callback. so I made before_create and coded build_resource; resource.build_user_profile; resource.user_profile.language_id = 45; inside. but it didn't work:( – HUSTEN Dec 13 '12 at 11:13
  • it still won't change the value when nested record is being created – HUSTEN Dec 13 '12 at 14:40