0

I have a User table with common fields: name, age, gender etc... In my application, when a new user signs up I want to check (approve or reject) his bio and profile photo to validate his account. I'm using ActiveAdmin and I wanted to have something like a scope in my User index where I see the "unchecked" user (I have done that part) and an action link(next to View, Edit and Delete) to approve or reject the bio/photo.

My problem is with this action link, I don't know how to do it. I've seen member_actions for custom actions, but I don't understand how it works and if I can modify one field one my user. Some help would be welcome on this.

mhatch
  • 4,441
  • 6
  • 36
  • 62
Charles Duporge
  • 622
  • 2
  • 12
  • 30
  • 2
    You can update an attribute with the [`update_attribute(name, value)` method](http://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-update_attribute). You should be able to use the [member actions](http://activeadmin.info/docs/8-custom-actions.html#member-actions) to add a custom action to the user controller and path. Instead of locking the user you could update various attributes and delete their photo and bio. There's also an example in the docs of using if with a proc to only display this to admins (depends on your setup I suppose). – user3366016 Aug 22 '16 at 16:29

1 Answers1

0

ActiveAdmin lets you specify a custom action like approve

ActiveAdmin.register User do

  member_action :approve, method: :put do
    resource.approve!
    redirect_to resource_path, notice: "Approved!"
  end

end

Create a method in your Model to do what's necessary to approve, for example if approved is a boolean in your table, then

class User < ActiveRecord::Base
  def approve!
    update(approved: true)
  end
end
SteveTurczyn
  • 36,057
  • 6
  • 41
  • 53
  • Thanks but there is no action link or something i can click on to call my method. Is there something else i need to do to for this? – Charles Duporge Aug 23 '16 at 08:44
  • @CharlesDuporge when you run `rake routes` you don't see an `admin/users/:id/approve` path? The member_action should do this for you. In your view you should be able to insert a `link_to admin_user_approve_path(@user)` (or something close to this). – user3366016 Aug 23 '16 at 13:24
  • @user3366016 I don't see `admin/users/:id/approve` when running `rake routes` i only get the defaults routes. Maybe i need to run some command to generate the routes? (By the way, what means the `!` in `approve!` ? I can't figure out what it is used for) – Charles Duporge Aug 25 '16 at 09:58
  • @CharlesDuporge the shebang(!) Can mean a few things but in this case is just a naming convension to show this method is directly updating a value; so a bit of a warning. Heres a great [post](http://stackoverflow.com/a/612653/3366016) on it. The route should be added by active admin. Can you update your question with your code snippets of these sections? How youre calling the link in the view and the actions. – user3366016 Aug 25 '16 at 11:29
  • @CharlesDuporge also your routes.rb would be helpful. Mostly I'm curious if you have `resources :users` or if you've possibly modified this here or somewhere else. – user3366016 Aug 25 '16 at 12:48