Nested model not available in Devise views

好久不见. 提交于 2019-12-11 23:50:18

问题


I'm having a problem with associations not being available in my views.

My models are:

:user has_many :subscriptions
:subscription belongs_to :user

I'm using Devise to manage authentication etc. for Users

What I'd like to do: when creating a new user in the registration process, I also want to also create a subscription for that user. Since Devise::RegistrationsController#new by default does not initialize an associated subscription, I've created my own RegistrationsController:

class RegistrationsController < Devise::RegistrationsController
  def new
    super
    resource.subscriptions.build
    logger.debug resource.subscriptions.inspect
  end
end

The debug statement there confirms that a Subscription object is successfully created:

[#<Subscription id: nil, user_id: nil, chargify_subscription_id: nil, chargify_product_handle: nil, created_at: nil, updated_at: nil>]

The problem: in the view, resource.subscriptions does not exist. If I inspect resource in the view, I get a User object that includes all of its own attributes but no associations (it should have an associated subscriptions)

debug(resource) gives the following:

--- !ruby/object:User
attributes:
  name:
  encrypted_password: ""
  created_at:
  updated_at:
  last_sign_in_ip:
  last_sign_in_at:
  sign_in_count: 0      last_name:
  current_sign_in_ip:
  reset_password_token:
  current_sign_in_at:
  remember_created_at:
  reset_password_sent_at:
  chargify_customer_reference:
  first_name:
  email: ""
attributes_cache: {}

changed_attributes: {}

destroyed: false
marked_for_destruction: false
new_record: true
previously_changed: {}

readonly: false

Is there something I'm missing, or perhaps is there something strange about the resource mechanism used by Devise that prevents associations from being available in the view?

Thanks!

Edit: If I just add resource.subscriptions.build in my view before rending the form, that works fine. But I think that kind of logic belongs in the controller and not the view, and I'd like to know what's keeping me from being able to put it there.


回答1:


This answer is really late, but I found that if I override the entire controller action "new" (instead of delegating some of it to the parent with "super"), then I can build the resource properly. The reason is because "super" renders the view before handing control back to your custom controller method. Long story short...

class RegistrationsController < Devise::RegistrationsController
  def new
    resource = build_resource({})  # as found in Devise::RegistrationsController
    resource.subscriptions.build
    respond_with_navigational(resource){ render_with_scope :new } # also from Devise
  end
end

Should work nicely...at least it did for me. Your code got me started on the right track anyway.



来源:https://stackoverflow.com/questions/6005932/nested-model-not-available-in-devise-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!