How to set the user to current_user when creating a new model using rails_admin
-
17-01-2021 - |
题
I have a rails 3.0.10 project I'm building using the rails_admin gem to some basic administrative functions. For one of the models being managed in rails_admin, I'd like to be able to set the user association based on the current_user. I would think this should be a fairly common requirement, so I'm sure that I'm missing something obvious.
I did find one post that seemed to solve this, but it's not working for me. So I was hoping someone else out there might have another suggestion.
This is the conversation I was able to find: http://groups.google.com/group/rails_admin/browse_thread/thread/ce0e22aeec1f72b7
解决方案
In case anyone is still interested, there is an workaround for this on Rails Admin's wike page: https://github.com/sferik/rails_admin/wiki/How-to-set-default-values
config.model Post do
edit do
field :user_id, :hidden do
default_value do
bindings[:view]._current_user.id
end
end
end
end