嵌套模型在设计视图中不可用
我遇到的问题是关联在我的视图中不可用。
我的模型是:
: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< /code> 对象创建成功:
[#<Subscription id: nil, user_id: nil, chargify_subscription_id: nil, chargify_product_handle: nil, created_at: nil, updated_at: nil>]
问题:视图中,resource.subscriptions
不存在。 如果我在视图中检查 resource
,我会得到一个 User
对象,该对象包含其自己的所有属性,但没有关联(它应该有一个关联的 subscriptions
>)
debug(resource)
给出以下内容:
--- !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
是否有我遗漏的东西,或者 Devise 使用的 resource
机制是否有一些奇怪的地方,阻止关联在视图中可用?
谢谢!
编辑: 如果我在渲染表单之前在视图中添加 resource.subscriptions.build
,效果就很好。但我认为这种逻辑属于控制器而不是视图,我想知道是什么阻止我将它放在那里。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个答案确实很晚了,但我发现如果我覆盖整个控制器操作“new”(而不是用“super”将其中一些操作委托给父级),那么我可以正确构建资源。原因是因为“super”在将控制权交还给自定义控制器方法之前渲染视图。长话短说……
应该很好用……至少对我来说是这样。无论如何,你的代码让我开始走上正轨。
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...
Should work nicely...at least it did for me. Your code got me started on the right track anyway.