Current User ID

751 views
Skip to first unread message

newtriks

unread,
Oct 26, 2011, 4:04:20 PM10/26/11
to rails_admin
Hi,

I would like to associate the current_user.id with records created and
am struggling to find out where the best place is to do this i.e.
should I create a controller for Record and associate the
current_user.id there or is there a method more in line with
rails_admin dsl to achieve this?

Cheers,

Simon

newtriks

unread,
Oct 26, 2011, 6:32:18 PM10/26/11
to rails_admin
Ok so it seems to work if I do the following:

config.model Library do
edit do
field :user_id do
#hide
def value
bindings[:view]._current_user.id
end
end
end
field :name
end

However, uncommenting 'hide' does indeed hide the form field but I
then lose my user_id assignment?

Benoit Bénézech

unread,
Oct 27, 2011, 8:25:58 AM10/27/11
to rails...@googlegroups.com
hide does not hide the field in a form HTML meaning :D


config.model Library do
    edit do
      field :user_id do
        #hide
        def value
          bindings[:view]._current_user.id
        end
        def render
          bindings[:view].form.hidden(self.method_name, self.html_attributes)

Simon Bailey

unread,
Oct 27, 2011, 8:27:28 AM10/27/11
to rails...@googlegroups.com
Hah! Gotcha thanks Benoit!

--
You received this message because you are subscribed to the Google Groups "rails_admin" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rails_admin/-/cPcZg-5awnwJ.

To post to this group, send email to rails...@googlegroups.com.
To unsubscribe from this group, send email to rails_admin...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rails_admin?hl=en.

newtriks

unread,
Oct 27, 2011, 5:18:12 PM10/27/11
to rails...@googlegroups.com
Hmmm that doesn't work, but I this gets me nearly there BUT now once again doesn't send the current_user.id with the submitted form:

config.model Library do
  edit
do
    field
:user_id do

     
# This hides the field label
      label
:hidden => true

     
def value
        bindings
[:view]._current_user.id
     
end

     
# This hides the field input and helper text but lose _current_user.id value
     
def render
        bindings
[:view].tag(:div, {:hidden => true})

newtriks

unread,
Oct 27, 2011, 6:03:46 PM10/27/11
to rails...@googlegroups.com
Of course this will not work, doh! 

Benoit Bénézech

unread,
Oct 28, 2011, 3:51:57 AM10/28/11
to rails...@googlegroups.com
Sorry, you have a much easier way of achieving this:

use the view_helper instance option with value 'hidden_field'.

config.model Library do
  edit
do
    field
:user_id do
      label :hidden => true

     
value do
        bindings
[:view]._current_user.id
     
end

     
view_helper do
       
:hidden_field

Simon Bailey

unread,
Oct 28, 2011, 4:06:52 AM10/28/11
to rails...@googlegroups.com
Benoit your a star thanks that works great! Works great and offers a further insight into how to manage views in rails_admin excellent thanks!

Simon

--
You received this message because you are subscribed to the Google Groups "rails_admin" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rails_admin/-/xB1A5AKCWP4J.

newtriks

unread,
Oct 28, 2011, 4:53:51 AM10/28/11
to rails...@googlegroups.com
So final result with the only yuk bit being the helper text aspect:

config.model Library do
  edit
do
    field
:user_id do

     
# This hides the field label
      label
:hidden => true

     
# This hides the help field *yuk*
      help
""

     
def value
        bindings
[:view]._current_user.id
     
end
     
# This hides the field input

Ajay Kumar

unread,
Dec 20, 2011, 1:38:12 AM12/20/11
to rails...@googlegroups.com
Has something changed in rails admin because of which this trick is not working anymore?

I had used this tip in RA version a311e3b833. Now I am trying to update RA to the new version and its not picking up current_user.id any more.

When I look at the params that are passed user_id is set an empty string.

Daniel Staudigel

unread,
Dec 20, 2011, 10:40:32 PM12/20/11
to rails...@googlegroups.com
I found out what commit caused it:

78494f5a11c1421cde4b4667a85c05ee8c0442f5

It looks like simply reverting this change and fixing up the merge problems fixes this problem.  You can do it yourself, or I have pushed the change at 


Happy Hacking!

Dan


--
You received this message because you are subscribed to the Google Groups "rails_admin" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rails_admin/-/JCnXyF7FIFMJ.

Benoit Bénézech

unread,
Dec 21, 2011, 6:54:39 AM12/21/11
to rails...@googlegroups.com
Hello, 

Overriding value function was untested and hackish, I added :default_value for that purpose. Also added hidden field. (won't show label, etc)

Reply all
Reply to author
Forward
0 new messages