Rails: activeadmin overriding create action

后端 未结 4 1085
渐次进展
渐次进展 2021-01-30 12:34

I have an activeadmin resource which has a belongs_to :user relationship.

When I create a new Instance of the model in active admin, I want to associate the currently lo

相关标签:
4条回答
  • 2021-01-30 13:06

    As per the AA source code this worked for me:

    controller do
      def call_before_create(offer)
      end
    end
    
    0 讨论(0)
  • 2021-01-30 13:10

    Another option:

    def create
      params[:item].merge!({ user_id: current_curator.id })
      create!
    end
    
    0 讨论(0)
  • 2021-01-30 13:13

    I ran into a similar situation where I didn't really need to completely override the create method. I really only wanted to inject properties before save, and only on create; very similar to your example. After reading through the ActiveAdmin source, I determined that I could use before_create to do what I needed:

    ActiveAdmin.register Product do
      before_create do |product|
        product.creator = current_user
      end
    end
    
    0 讨论(0)
  • 2021-01-30 13:13

    You are right active admin use InheritedResources, all other tools you can see on the end of the page.

    0 讨论(0)
提交回复
热议问题