Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding actions to dashboard #222

Closed
gjackson12 opened this issue Nov 12, 2015 · 17 comments
Closed

Adding actions to dashboard #222

gjackson12 opened this issue Nov 12, 2015 · 17 comments
Labels
dashboards how administrate presents fields and displays data feature new functionality that’s not yet implemented

Comments

@gjackson12
Copy link

Is there a way to add custom actions to the dashboard view? Right now I just see edit, and destroy. Along those actions I would like to list a few other custom actions if possible.

@c-lliope
Copy link
Contributor

@gjackson12 this has been on my wishlist for a long time now. 😄

At the moment, you need to do it all from scratch - run rails generate administrate:views:index YourResourceName, add a link, and add a custom controller action.

I have a few ideas in mind for how we could improve this process, but I'd love to hear your thoughts - what API would you want for this?

@gjackson12
Copy link
Author

One other question. Let's say I added an action called "send_notification" to the Users resource. Do I need to create that route manually?

(thinking of some suggestions for the API)

@c-lliope
Copy link
Contributor

@gjackson12 yep - you'd need to create the route yourself to match your controller action.

@c-lliope
Copy link
Contributor

(that's how it works at the moment)

@gjackson12
Copy link
Author

So I added the following route:

post 'admin/users/:id/send_notification', to: "admin/users#send_notification", as: 'send_notification_admin_user'

I also put the following in my controller:

def send_notification
end

Finally, I have the following in the view:

 <td><%= link_to(
   t("administrate.actions.send_notification"),
   [:send_notification, Administrate::NAMESPACE, resource]
 ) %></td>

I keep getting the following error:

undefined method `send_notification_admin_user_path

Any help would be appreciated.

@c-lliope
Copy link
Contributor

hm... I think you might need to include a method: :post argument in your link_to helper.

@ariejan
Copy link

ariejan commented Nov 13, 2015

@gjackson12 are you defining that in the admin namespace? Your url helper would need to be prefixed with admin_ in that case. Check with rake routes if unsure.

@c-lliope
Copy link
Contributor

@gjackson12 ping. Any updates here?

@johnmeehan
Copy link

Just change the order of the link_to attributes to [Administrate::NAMESPACE, : send_notification, resource]

        <td><%= link_to(
            t("administrate.actions.send_notification"),
          [Administrate::NAMESPACE, : send_notification, resource],
          method: :post
        ) %></td>

This will generate your link route correctly.

In my routes.rb I added a 'clone' action by adding

  namespace :admin do
     DashboardManifest::DASHBOARDS.each do |dashboard_resource|
      resources dashboard_resource
    end
    post 'palladio_styles/:id/clone', to: "palladio_styles#clone", as: 'clone_palladio_style'

    root controller: DashboardManifest::ROOT_DASHBOARD, action: :index
  end

This gives me the additional route I need:
admin_clone_palladio_style POST /admin/palladio_styles/:id/clone(.:format) admin/palladio_styles#clone

@joaodiogocosta
Copy link

joaodiogocosta commented Aug 12, 2016

@gjackson12, this is my slightly different approach:

namespace :admin do
  DashboardManifest::DASHBOARDS.each do |dashboard_resource|
    resources dashboard_resource do

      if dashboard_resource == :palladio_styles
        member do
          post :clone
        end
      end

    end
  end

  root controller: DashboardManifest::ROOT_DASHBOARD, action: :index
end

And the link follows the save convention as CRUD ones:

 <td><%= link_to(
            t("administrate.actions.send_notification"),
          [:send_notification, Administrate::NAMESPACE, resource],
          method: :post
        ) %></td>

Not perfect, but still, cleaner than having to write all the stuff by hand Rails' already generating for you.

@carlosramireziii
Copy link
Collaborator

@gjackson12 Were you able to get your custom action and link working?

There are a couple of good approaches outlined above. I'm going to close up this ticket for now.

@bo-oz
Copy link

bo-oz commented Feb 26, 2020

Is there still any roadmap for Administrate? An easier API to add custom actions would be highly appreciated. I think generating the views for each model with custom actions, created unnecessary amounts of boilerplate. Would be great if there was a way to define the actions in the Dashboard or Controller class.

@pablobm pablobm added the feature new functionality that’s not yet implemented label Mar 5, 2020
@nickcharlton nickcharlton added the dashboards how administrate presents fields and displays data label Mar 26, 2020
@vizcay
Copy link

vizcay commented Oct 23, 2022

Hi, why is this closed? Is not a fair feature request to have in the issue tracker?

If is open someone with a bit of background may jump and do it.

@jabbett
Copy link

jabbett commented Feb 26, 2024

In my case, I wanted to update a record's field value with a single click. I only needed to customize my model's _collection.html.erb file, since I leveraged the existing update controller action.

In this example, my model is Author, and I'm trying to change it's status field to active :

<%= button_to(
  "Activate",
  admin_author_path(resource),
  method: :patch,
  params: { author: { status: 'active' } }
) %>

I hope this helps others!

@jesseduffield
Copy link

I've been taking a look at Administrate as an alternative to RailsAdmin and it looks like the ability to easily add custom actions is the main thing that's currently missing. In rails admin you can specify actions against models and those actions will show up next to the default routes like so:

image

You can implement each action either as immediately doing something upon clicking it, or having it first show a form and then doing something upon form submission.

Actions also automatically appear at the end of tables for easy access:

image

I think that actions should be considered a first-class entity in Administrate, just like they are in RailsAdmin, and I believe that Administrate can find a way to implement it that aligns with its value of using as much regular rails as possible.

I'm eager to switch over to Administrate because I much prefer its general approach to RailsAdmin but it sounds like this shortcoming is going to be too onerous to justify the switch.

@nickcharlton
Copy link
Member

@jesseduffield, could you open a new issue, and reference this one?

I think we should revisit this, as it has significant merit, but this particular issue has a lot of solutions which are likely not relevant any more and folks who are no longer so closely involved with Administrate.

@jesseduffield
Copy link

Sure thing, see #2743

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dashboards how administrate presents fields and displays data feature new functionality that’s not yet implemented
Projects
None yet
Development

No branches or pull requests