0

Example I want to create a dropdown of commands in an admin section. When the user selects one, such as

Purchase.all

User.where(:active => 1)

Basically any common commands that I would run in the console before a fully functional backend is created. I know this is a bit unconventional but im just looking for a rapid way to make some common admin queries available. Once the command has been run I'd like it display the exact output that would be displayed in the rails console. I've installed the Hirb gem which formats results in the console and would like that formatting to show when I output it in the view. Is this at all achievable?

1
  • @xdazz these commands will be checked against the controller so it is not solely based user input that that determines what runs. Also this is just a prototype for now. Commented Mar 6, 2014 at 8:45

1 Answer 1

2

Of course it is possible, just run the command in your action, render it with Hirb and pass the output to your view:

def action
  result = run_command(params[:command])

  Hirb::View.load_config
  Hirb::View.render_method = lambda { |output| @hirb_output = output }
  Hirb::View.render_output(result)
end

private

def run_command(command)
  case command
  when 'purchase_all'
    Purchase.all
  when 'active_users'
    User.where(:active => 1)
  end
end

And in your view:

<pre>
  <%= @hirb_output %>
</pre>

However, I would use ActiveAdmin or RailsAdmin instead.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Stefan, just what I was looking for! I'll take a look at those gems for the production version as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.