[英]Is it possible to run rails console commands in ruby controller/view and display the output
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.
一旦命令运行,我希望它显示将在rails控制台中显示的确切输出。 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.
我已经安装了Hirb gem,它在控制台中格式化结果,并希望在视图中输出时显示格式。 Is this at all achievable?
这完全可以实现吗?
Of course it is possible, just run the command in your action, render it with Hirb and pass the output to your view: 当然有可能,只需在你的动作中运行命令, 用Hirb渲染它并将输出传递给你的视图:
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. 但是,我会使用ActiveAdmin或RailsAdmin 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.