问题描述
我试图从控制器和活动的admin替代控制器中进行操作,但我无法使其正常工作。
I have tried to do it from the controller and from the active admin override controller and I can't make it work.
用户创建了一个网站。
current_user有一个id属性
网站有一个user_id属性
A user creates a website.current_user has an id attributewebsite has an user_id attribute
因此,当我创建一个新网站时,我想将current_user.id添加到网站中。用户身份。我不能
So when I create a new website I want to add the current_user.id into website.user_id. I can't.
有人知道吗?
现在,我在new / create动作中需要它,但在编辑/更新动作中也可能需要它。
Right now I need it on the new/create actions but I'll probably need this on the edit/update actions too.
推荐答案
这似乎对我有用:
ActiveAdmin.register Website do
controller do
# Do some custom stuff on GET /admin/websites/*/edit
def edit
super do |format|
# Do what you want here ...
@website.user = current_user
end
end
end
end
您应该能够以相同的方式覆盖其他控制器操作。
You should be able to override other controller actions in the same way.
这篇关于Rails 3 Active Admin将预设值添加到新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!