Rails: activeadmin overriding creates an action

I have an activeadmin resource that has own_to: user relationship.

When I create a new instance of the model in the active admin, I want to associate the registered user as the user who created the instance (quite standard material that I would imagine).

So ... I got it with:

controller do def create @item = Item.new(params[:item]) @item.user = current_curator super end end 

However;) I'm just wondering how this works? I just hoped that assigning the @item variable to the user and then calling super would work (and that would happen). I also began to study the gem, but could not understand how it works.

Any pointers would be great. I assume this is what InheritedResources gives you?

Thank!

+42
ruby-on-rails activeadmin inherited-resources
Dec 03 '12 at 1:01
source share
4 answers

I came across a similar situation where I really don't need to completely override the create method. I really only wanted to add properties before saving, and only when creating; very similar to your example. After reading the ActiveAdmin source, I decided that I could use before_create to perform the necessary actions:

 ActiveAdmin.register Product do before_create do |product| product.creator = current_user end end 
+86
Apr 7 '14 at 18:33
source share

Another option:

 def create params[:item].merge!({ user_id: current_curator.id }) create! end 
+11
Oct 09 '13 at 16:13
source share

You are using admin InheritedResources correctly, all other tools you can see at the end of the page .

+2
Dec 03
source share

According to AA source code, this worked for me:

 controller do def call_before_create(offer) end end 
0
Aug 17 '14 at 1:47
source share



All Articles