Assigning current_user before creating a model

766 views
Skip to first unread message

Ajay Kumar

unread,
Apr 9, 2011, 3:41:42 PM4/9/11
to rails...@googlegroups.com
Hi,

I have an model "Article" with association user. I don't want to show the user in when creating a article from rails_amdin and I have configured rails_admin.rb to not show it.

When I try to create a article, article is not getting saved because of the user association.

Right now I am thing of assigning current_user to Thread.local and adding before_create in the article model. I really don'e like this approach but its the only one that I know. Is there any elegant  solution in which I can assign the current_user to user field?

Thanks,
Ajay Kumar G

Petteri Kääpä

unread,
Apr 10, 2011, 2:38:29 PM4/10/11
to rails_admin
I haven't tested this, but assuming there's an belongs_to user
association with column user_id in your article model you could define
parse_input method (as defined in
RailsAdmin::Config::Fields::Types::Datetime) for user_id field which
gets run before save:

RailsAdmin.config Article do
edit do
group :default do
field :user_id do
hide
def parse_input(attributes)
user_id = get_user_id_somehow # metacode
attributes[:user_id] = user_id # attributes is the hash that
gets sent to your model instance's attributes= method
end
end
# You also have to declare all
# other fields to make them visible
# as by default only declared fields
# are shown if any are declared...
end
end
end

Would that work out for you?

Ajay Kumar

unread,
Apr 10, 2011, 7:31:38 PM4/10/11
to rails...@googlegroups.com
Kaapa,

This trick seems to work except that I am not able to find the appropriate logic for 'get_user_id_somehow' method.

Is there any way in which I can determine the current_user in initialization code?

Thanks,
Ajay kumar G

Greg Fitzgerald

unread,
Apr 11, 2011, 2:42:16 PM4/11/11
to rails_admin
> Is there any way in which I can determine the current_user in initialization
> code?

Trying to figure this out today myself. I think the easiest way to
handle it is to just do this.

def parse_input(attributes)
attributes[:user_id] = RailsAdmin::DEFAULT_CURRENT_USER
end

The RAILSADMIN::DEFAULT_CURRENT_USER comes from here
https://github.com/sferik/rails_admin/blob/master/lib/rails_admin.rb.

Ajay Kumar

unread,
Apr 12, 2011, 8:20:39 AM4/12/11
to rails...@googlegroups.com
Hi Greg,

Thanks for taking time to answer this question. 

I have tried this method but I don't think this method would work. When you create a record this way, user_id will always be 1 but not the current_user_id.

Thanks,
Ajay Kumar G

Ajay Kumar

unread,
May 4, 2011, 1:49:12 AM5/4/11
to rails...@googlegroups.com
Guys,

I was not able solve this problem yet.

Right now this is what I have

      field :user_id do
        hide
        def parse_input(attributes)
          attributes[:user_id] = RailsAdmin::DEFAULT_CURRENT_USER
        end
      end
 
but this does not work as for some reason RailsAdmin::DEFAULT_CURRENT_USER is always returned as 1.

Any help will be greatly appreciated.

Thanks,
Ajay

Petteri Kääpä

unread,
May 4, 2011, 6:56:08 AM5/4/11
to rails_admin
RailsAdmin::DEFAULT_CURRENT_USER is actually a Proc object, so it'd
need to be evaluated to return a value. Unfortunately that wouldn't
work either as it meant to be used in the scope of the controller or
view (it uses the request object only available in that context).

I think I gave poor advice to begin with. Perhaps a cleaner approach
would be to define user_id as a hidden input field with default value
of current user. Something like this (untested):

...
field :user_id
# Set the type of form helper to use
view_helper :hidden_field
# Use the generic partial instead of the belongs_to one so proper
view_helper gets used
partial :form_field
# Override value method for the eigenklass of this field's instances
in create and update views
def value
bindings[:view]._current_user.id
end
end
...

It's still very hacky, so I'll try to think of a better way to solve
this kind of stuff in the upcoming renovation of config dsl.

P.

Ajay Kumar

unread,
May 4, 2011, 8:12:07 PM5/4/11
to rails...@googlegroups.com
Thanks a lot Kaapa. It works like a charm.

stephen murdoch

unread,
May 7, 2011, 5:17:57 PM5/7/11
to rails_admin
Thanks for the code Petteri, it more or less solves my problem too,
but I have a quick question if you don't mind. My code is as follows:

config.model Post do
edit do
field :user_id do
view_helper :hidden_field
partial :form_field
def value
bindings[:view]._current_user.id
end
end
end
end

This works well except for the fact that I can't seem to hide the
label that is associated with the user_id field.

Using your code I can hide the field itself, but not the label that
reads "User" which appears next to the input the hidden input.

Is there anyway to hide an input's associated label too (and not just
the input itself) so that it's not confusing to the user? Is there
something wrong with my code?

Thanks in advance

stephen murdoch

unread,
May 7, 2011, 6:02:28 PM5/7/11
to rails_admin
I think I solved this by adding the following two lines:

label ""
help ""

Solved - probably

On May 7, 10:17 pm, stephen murdoch <stephenjamesmurd...@gmail.com>
wrote:

Bruno Arueira

unread,
Jul 1, 2011, 3:21:14 PM7/1/11
to rails...@googlegroups.com
Stephen,

I applied your solution on my app but the help disappeared and the label no!

Only difference between your solution and mine is an action to handle the field, put my handle on create. Can you suggest something?

Thanks.

Benoit Bénézech

unread,
Dec 21, 2011, 6:54:51 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)

Ajay Kumar

unread,
Dec 22, 2011, 2:38:34 AM12/22/11
to rails...@googlegroups.com
Thanks a lot benoit. Was able to successfully upgrade my application to the latest version of RA.
Reply all
Reply to author
Forward
0 new messages