如何在RubyMine中运行scratch文件或在Rails环境中运行IDEA ?

时间:2021-06-14 20:55:32

I want to use scratch files as a console replacement for prototyping in my Rails apps. The simplest solution I've found is requiring environment manually like this:

我想使用scratch文件作为控制台替换Rails应用程序中的原型。我发现的最简单的解决方案是像这样手工要求环境:

require '/project/path/config/environment.rb'

But it does not use Spring and is terribly slow because of that.

但它不使用弹簧,因此速度非常慢。

1 个解决方案

#1


7  

I've found two ways to do it, depending on your workflow one might fit you better than another. Assuming you're using IDEA 14 (it might be different for earlier versions) and Rails 4.1+ or have Spring installed.

我找到了两种方法来实现它,这取决于您的工作流程,它可能比另一个更适合您。假设您使用的是IDEA 14(对于早期版本可能有所不同)和Rails 4.1+或安装了Spring。

Custom runner

  1. In top menu Run -> Edit Configurations...;
  2. 在top菜单中运行->编辑配置…
  3. Configuration tab:
    2.2. Ruby script: <Path to your scratch file>;
    2.3. Working directory: <Your project dir>;
    2.4. Environment variables: RAILS_ENV=development;
    2.5. Ruby arguments: -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) <Your project dir>/bin/spring rails runner;
    2.6. Ruby SDK: <Your project SDK>.
  4. 配置选项卡:2.2。Ruby脚本: <您的scratch文件> 的路径;2.3。工作目录: <您的项目目录> ;2.4。环境变量:RAILS_ENV =发展;2.5。Ruby参数:-e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) <您的项目dir> /bin/spring rails runner;2.6。Ruby SDK: <您的项目sdk> 。
  5. Bundler tab:
    3.1. Run the script in context of the bundle (bundle exec): <check>.
  6. 打包机选项卡:3.1。在bundle (bundle exec)上下文中运行脚本:

The problem with this approach is that you would have to manually change path to scratch file each time you want to run the different one (please comment if you know workaround).

这种方法的问题是,每次您想要运行不同的文件时,您都需要手工修改路径来创建文件(如果您知道变通的话,请评论)。

External tool

  1. In top menu IntelliJ IDEA -> Preferences;
  2. 在顶部菜单IntelliJ IDEA—>选项;
  3. Tools -> External Tools;
  4. 工具- >外部工具;
  5. Hit + in the bottom of the menu:
    3.1. Name: Rails Runner;
    3.2. Program: $ProjectFileDir$/bin/spring;
    3.3. Parameters: rails runner $FilePath$;
    3.4. Working directory: $ProjectFileDir$.
  6. 在菜单的底部打+。名称:Rails跑步者;3.2。计划:ProjectFileDir美元/ bin /春天;3.3。参数:rails跑步FilePath美元;3.4。工作目录:ProjectFileDir美元。
  7. Keymap:
    4.1. Search for Rails Runner;
    4.2. Double-click and add custom shortcut (Alt + S is convenient and available).
  8. Keymap:4.1。寻找Rails跑步者;4.2。双击并添加自定义快捷方式(Alt + S方便且可用)。

The only problem with this approach is that you have to have custom shortcut to make it convenient.

这种方法的唯一问题是,您必须有自定义的快捷方式来方便使用。

#1


7  

I've found two ways to do it, depending on your workflow one might fit you better than another. Assuming you're using IDEA 14 (it might be different for earlier versions) and Rails 4.1+ or have Spring installed.

我找到了两种方法来实现它,这取决于您的工作流程,它可能比另一个更适合您。假设您使用的是IDEA 14(对于早期版本可能有所不同)和Rails 4.1+或安装了Spring。

Custom runner

  1. In top menu Run -> Edit Configurations...;
  2. 在top菜单中运行->编辑配置…
  3. Configuration tab:
    2.2. Ruby script: <Path to your scratch file>;
    2.3. Working directory: <Your project dir>;
    2.4. Environment variables: RAILS_ENV=development;
    2.5. Ruby arguments: -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) <Your project dir>/bin/spring rails runner;
    2.6. Ruby SDK: <Your project SDK>.
  4. 配置选项卡:2.2。Ruby脚本: <您的scratch文件> 的路径;2.3。工作目录: <您的项目目录> ;2.4。环境变量:RAILS_ENV =发展;2.5。Ruby参数:-e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) <您的项目dir> /bin/spring rails runner;2.6。Ruby SDK: <您的项目sdk> 。
  5. Bundler tab:
    3.1. Run the script in context of the bundle (bundle exec): <check>.
  6. 打包机选项卡:3.1。在bundle (bundle exec)上下文中运行脚本:

The problem with this approach is that you would have to manually change path to scratch file each time you want to run the different one (please comment if you know workaround).

这种方法的问题是,每次您想要运行不同的文件时,您都需要手工修改路径来创建文件(如果您知道变通的话,请评论)。

External tool

  1. In top menu IntelliJ IDEA -> Preferences;
  2. 在顶部菜单IntelliJ IDEA—>选项;
  3. Tools -> External Tools;
  4. 工具- >外部工具;
  5. Hit + in the bottom of the menu:
    3.1. Name: Rails Runner;
    3.2. Program: $ProjectFileDir$/bin/spring;
    3.3. Parameters: rails runner $FilePath$;
    3.4. Working directory: $ProjectFileDir$.
  6. 在菜单的底部打+。名称:Rails跑步者;3.2。计划:ProjectFileDir美元/ bin /春天;3.3。参数:rails跑步FilePath美元;3.4。工作目录:ProjectFileDir美元。
  7. Keymap:
    4.1. Search for Rails Runner;
    4.2. Double-click and add custom shortcut (Alt + S is convenient and available).
  8. Keymap:4.1。寻找Rails跑步者;4.2。双击并添加自定义快捷方式(Alt + S方便且可用)。

The only problem with this approach is that you have to have custom shortcut to make it convenient.

这种方法的唯一问题是,您必须有自定义的快捷方式来方便使用。