嵌套模型在Devise视图中不可用 [英] Nested model not available in Devise views

查看:156
本文介绍了嵌套模型在Devise视图中不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的模型是:

 :user has_many:subscriptions 
:subscription belongs_to:user

我正在使用Devise管理用户的身份验证等。



我想做什么:在注册过程中创建新用户时,我也想要为该用户创建订阅。
由于 Devise :: RegistrationsController#new 默认情况下不会初始化关联的订阅,我已经创建了自己的 RegistrationsController

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

该调试声明确认已成功创建订阅对象:

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

问题:在视图中, resource.subscriptions 不存在。
如果我在视图中检查资源,我得到一个包含其所有属性的用户对象但没有关联(它应该有一个关联的订阅



debug(resource) / code>给出以下内容:

  ---!ruby / object:用户
属性:
名称:
encrypted_pa​​ssword:
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:
电子邮件:
attributes_cache:{}

changed_attributes:{}

已销毁:false
标记为forDestruction:false
new_record:true
以前的= {}

readonly:false

有没有什么我错过的,对于Devise使用的资源机制有什么奇怪的可以阻止关联在视图中可用?



谢谢!



编辑:
如果我只是在我的视图中添加 resource.subscriptions.build 形式,工作正常。但是我认为这种逻辑属于控制器而不是视图,我想知道什么让我无法把它放在那里。

解答方案

这个答案真的很晚了,但是我发现如果我覆盖整个控制器动作new(而不是将其中的一些委托给super),那么我可以构建资源正常原因是因为超级在将控制权交给您的自定义控制器方法之前呈现视图。长篇小说...

  class RegistrationsController< Devise :: RegistrationsController 
def new
resource = build_resource({})#在Devise :: RegistrationsController中找到
resource.subscriptions.build
respond_with_navigational(resource){render_with_scope:new }#也来自Devise
end
end

应该很好地工作...至少对我来说你的代码让我开始正确的轨道。


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.

解决方案

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.

这篇关于嵌套模型在Devise视图中不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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