Custom Tool - perform action
# avo-2
n
hey @lemon-wall-20836 , I'm trying to create a custom tool called `reset stats`; where I'll have a button that runs code;
Copy code
ruby
class Avo::ToolsController < Avo::ApplicationController
  def stats_fixer
    @page_title = "Stats fixer"
    add_breadcrumb "Stats fixer"
  end

  def process_stats_fixer
    Parallel.each(User.all) do |user|
      user.update_caching_data!
    rescue StandardError => e
      Rails.logger.error "#{DateTime.now} - Error in fixer_user_stats - #{e}"
    end
  end
end
Copy code
ruby
scope :admin do
  get "stats_fixer", to: "avo/tools#stats_fixer"
  post 'process_stats_fixer', to: 'avo/tools#process_stats_fixer'
end
what's the easiest way of rendering the button to call this action ? I'm getting weird results every time
m
Thread automatically created by roc.khalil in #740893011994738751
l
Hello @narrow-electrician-89982 the easiest way would be to render it on the menu section. Since you want it as an action and not actually render something you can insert the button link whenever you want. What are those "weird results"?
n
since the action is a post, I need to - maybe - redirect the page back with a success message
or like show a success message on the page
so the post is performing the action, but the page is not always updating
do you guys have any working example of such thing ?
l
So if i understand it correclty you have a page with some data, that page is a custom tool, and you want to render an button to refresh the data on the page?
n
it doesn't have any data
it's just a button to run a job, that job fixes data on the user level
this is what I did, I guess it's a working solution:
Copy code
ruby
def stats_fixer
  @page_title = "Stats fixer"
  add_breadcrumb "Stats fixer"
end

def process_stats_fixer
  Parallel.each(User.all) do |user|
    user.update_caching_data!
  rescue StandardError => e
    Rails.logger.error "#{DateTime.now} - Error in fixer_user_stats - #{e}"
  end

  return redirect_to '/admin/stats_fixer', notice: "Stats fixer done!"
end
Copy code
ruby
<% c.with_tools do %>
  <%= a_link('/admin/process_stats_fixer', icon: 'heroicons/outline/finger-print', style: :primary, is_link: true) do %>
    Reset stats
  <% end %>
<% end %>
so basically I'm doing a get
process_stats_fixer
, where I run the code, and then the page redirects with a notice
it works... but I know there's a better solutiong that can be implemented
l
Ahh got it,
flash[:notice] = "Stats fixer done!"
inside
process_stats_fixer
and render
stats_fixer
as response would not need the extra request i think
n
if it's a
post
it wouldn't need it ( I guess ) ; but since `post`ing will only work with forms, I did it as a get and redirect
sometimes, if it works, it works... 😂
thanks for jumping in quickly btw, appreciate it 🤝
l
Try this:
Copy code
ruby
<%= a_link('/admin/process_stats_fixer', icon: 'heroicons/outline/finger-print', style: :primary, is_link: true, method: :post) do %>
Copy code
ruby

  def process_stats_fixer
    Parallel.each(User.all) do |user|
      user.update_caching_data!
    rescue StandardError => e
      Rails.logger.error "#{DateTime.now} - Error in fixer_user_stats - #{e}"
    end

    flash[:notice] = "Stats fixer done!"

    respond_to do |format|
      format.turbo_stream do
        render partial: "avo/partials/flash_alerts"
      end
    end
  end
n
Copy code
sh
Missing partial avo/partials/_flash_alerts
too soon
😄
avo version?
n
2
l
.
point what?
n
2.36
let me update it to the latest
l
Should work on
2.43
do you intent to upgrade soon?
n
perfect; works on 2.43 !
Now the gif fits! 😄
n
haahahahah
💯
thanks for the quick help ❤️
l
anytime, happy coding!