Question

I have the following:

collection_action :new, :method => :post do
  begin
    user = User.find_by_email(params[:email])
    if user
      UserPermission.create(:user_id => user.id,
      :permission => UserPermission::SUPPORT,
      :creator => current_user)
    end
    rescue ActiveRecord::RecordNotFound
      flash[:warn] = 'User not found'
  end
  redirect_to admin_support_users_path, notice: 'Support user added.'
end

form do |f|
  f.inputs do
    f.input :email
  end
end

action_item only: [:index], :method => :post do
  link_to 'Add Support User', new_admin_support_user_path
end

The above works in the sense that no error is thrown. The support users page loads and I'm able to click the Add Support User button. However, 'Support user added.' is immediately shown. The Add Support User button does not take me to a form to enter an email. How do I add/create/use a form that passes an email parameter to my collection_action?

I'm new to activeadmin and documentation is sparse, so any help is appreciated. Thanks.

Était-ce utile?

La solution

Figured it out. I'll try to explain as I understand it. And my initial question may have been unclear. The reason I was getting the, 'Support user added.' message is because I was updating the wrong method. The method above should have been the :create controller method, not the :new controller method. :new uses HTTP GET, which is why it would go directly to the redirect. :create accepts an HTTP POST. So, instead, I have the following:

def create
  begin
    user = User.find_by_email(params[:email])
    if user
      UserPermission.create(:user_id => user.id,
      :permission => UserPermission::SUPPORT,
      :creator => current_user)
    end
  rescue ActiveRecord::RecordNotFound
    flash[:warn] = 'User not found'
  end
  redirect_to admin_support_users_path, notice: 'Support user added.'
end

def new 
  render 'new.html.arb', :layout => 'active_admin'
end

And this correctly creates a nice looking active admin form, accepting an email parameter.

Autres conseils

You just need to add another action--just like a normal resource needs separate actions for create and new. Your 'new' action can render a custom form either inline or in a separate partial, as shown here:

http://www.activeadmin.info/docs/5-forms.html

That said, I'm not sure I understand why you need a custom action. Is this in your User resource file in active admin? If so you can just use the default new user action and include the current user in the form as a hidden variable as the creator. If this is not in your User resource active admin file then you probably need one.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top