Why my rails command works differently with system() than in terminal? - ruby-on-rails

The command that I'm trying to run is rails _3.2.13_ new App
When I run it in command line it creates a Rails app with version 3.2.13, but when I run it with system "rails _3.2.13_ new #{self.name} -T -B" it creates a Rails app with the latest version of Rails not 3.2.13 version.

This is a result of Ruby using /bin/sh to execute shell commands, whereas you are probably using /bin/bash in Terminal on a daily basis. The way each is loaded and the specific configurations present in each will alter the configuration.
If you run which rails from both calls to system and in your terminal you'll likely see different paths. Check echo $PATH and you'll likely see different results too.
To resolve the situation, you can check out What's the difference between .bashrc, .bash_profile, and .environment? which will give you a much better understanding of what's going on, then adjust your shell configuration to accomodate.

Related

Rails 5: How Do I Set Up a Merge Tool to use for rails app:update?

I'm upgrading a rails 5.2.4.1 app to rails 6. There are a few config files I want to merge instead of manually adding information from a copy of the old file.
The first time I entered m to merge the file I got the following message.
Please specify merge tool to `THOR_MERGE` env.
I did a search and found this blog post. The folder that this person found after Googling does not exist on my Mac computer.
FileMerge doesn't exist and DiffMerge is very old. I haven't found any information about using one with rails app:update.
What Mac merge tools are currently used that I can set the env var THOR_MERGE to?
Stumbled upon this answer searching for the same thing.
You can launch vscode diff tool by setting the THOR_MERGE env variable as follows:
THOR_MERGE="code -d $1 $2"
This is assuming you have code in your PATH, which you can setup by following the instructions here.
It seems that XCode includes /usr/bin/opendiff, which is a binary that launches FileMerge.app. So I was able to:THOR_MERGE=opendiff rails app:update
RubyMine can also be used as the merge tool. To make that work on my macOS Monterey system, I created a new Bash script at /usr/local/bin/rubymine-merge (based in part on the rubymine script provided by JetBrains Toolbox) with the following code:
#!/usr/bin/env bash
declare -- wait="-W"
bundle exec rubocop --server -A "$1"
open -na "$HOME/Library/Application Support/JetBrains/Toolbox/apps/RubyMine/ch-0/222.3739.56/RubyMine 2022.2 EAP.app/Contents/MacOS/rubymine" $wait --args merge "$2" "$1" "$2"
After this, I was able to invoke the Rails update script as follows:
THOR_MERGE=rubymine-merge bin/rails app:update
This is working as of RubyMine 2022.2.1 in mid-August of 2022.
The Rails update script seems to require that the merge tool behave as if it had received a --wait parameter, because after the last merge the script deletes all temporary files. Passing --wait as a command-line parameter seems problematic with the 2022.2 version of the command-line script, so I hard-coded the -W on line 3.
I also inserted a call to rubocop on line 5 so that the proposed changes from the Rails app:update script would already be aligned with the standards for this project. Skip that part if it's not meeting your needs. I had to use bundle exec rubocop instead of bin/rubocop because the Rails app:update script is not always running in the context of the root directory of your project.
It is suboptimal for this script be separate from the /usr/local/bin/rubymine script that is generated by JetBrains Toolbox. Every time a new version of RubyMine is installed, the path to the actual RubyMine application can change.
Simple Solution:
THOR_MERGE=kdiff3 rails app:update
or use opendiff / meld / kdiff etc.
Credit to #pduey (thanks).

Redmine plugin creation - Could not find generator 'redmine_plugin'

I run Redmine 3.4 with Rails (5.2.0) and Docker 18.03.1-ce on Ubuntu 16.04 Xenial (which is new for me), following this GitHub repository: https://github.com/sameersbn/docker-redmine
I create my Rails app in the same folder where the docker-compose.yml has been created, and cd to it.
Then I have the exact same problem than described in this Redmine post (http://www.redmine.org/boards/3/topics/48309?r=48507#message-48507): when I try the command rails generate redmine_plugin Plug_test, this two error messages appear:
Running via Spring preloader in process 32109
Could not find generator 'redmine_plugin'
So I try the commands that Keith suggested, and running the generate command again, the Spring error message disappear, but the generate command still doesn't work (Could not find generator 'redmine_plugin').
Any idea what to do? I don't know if I'm going in the right direction.
Thanks a lot for your help.
well simple problem, you're running command from outside of your redmine app directory, you need to go into your redmine app directory, after that you can run rails generate redmine_plugin Plugin_test from there
As Ravi mentioned above, you need to go into your redmine app directory instead of your rails app directory.
Or, maybe you can exec plugin generate command via docker run command.
# e.g. In case plugin name is “myplugin"
docker run --name=redmine -it --rm \
--volume=/srv/docker/redmine/redmine:/home/redmine/data \
sameersbn/redmine:3.4.4-2 \
app:rails generate redmine_plugin myplugin
If this works fine, plugin directory named “myplugin” will be generated under /srv/docker/redmine/redmine/plugins/ directory.
Personally, I think, you had batter not use docker to create and development Redmine’s plugin, especially if you are not familiar with Redmine and Docker so much.
I hope this would be any help.

New to Command Prompt - Do I need to prefix every command with "Jruby -S ..."

I'm new to using Window's Command Prompt, and also to developing with Ruby on Rails. Possibly a silly question but one that I'm sure everyone who learns with CodeCademy will end up asking; right now I'm prefixing every command for my project with 'Jruby -S ...", for example:
C:\users\MyName\MyProject> Jruby -S rails new MyApp
...
C:\users\MyName\MyProject> Jruby -S bundle install
...
C:\users\MyName\MyProject> Jruby -S rake db:migrate
Can I use some kind of alternative shell to save me typing Jruby -S every time? I'm aware of bash and powershell but have basically zero knowledge of whether I should be using them...
Thanks folks!
EDIT
Lots of helpful suggestions below, but I was really looking for a shell to mimic the functionality of the console on codecademy.com (which I believe is supposed to work like a Mac's 'bash' program?). Thanks anyway.
I'm new to using Window's Command Prompt
The CMD works very similarly to the GUI/Shell -- you have to call applications and then run commands with them.
The difference between CMD and windows is that CMD is "naked" - you have to ensure all the paths are correct, and that you're calling the correct application each time.
For example, calling rails server literally translates as:
Program = ruby.exe / rails
Command = server
CMD uses the PATH environment variable to make this process smoother.
The PATH var basically allows you to reference applications on your computer from the CLI (command line interface). This means that if you have an application (EG ruby.exe), you can add the ruby.exe directory to your PATH variable, allowing you to call ruby ... straight from cmd.
--
In your case, I don't have much experience with JRuby; I do know, however, that if you want to invoke the functionality of that application, you have to call it from the cli.
Hopefully my answer gives some context.
You can do that with powershell.
I'm sure that there should be a better way to do that, but you can try this
$ruby = "Jruby"
$s = "-S"
& $ruby $s rails new MyApp
I don't work on windows, however the jruby zip files on the download site have a bin directory with .bat and .exe files for jruby, rake, and gem. You could just add the directory you installed jruby to and the 'bin' subdirectory to your PATH to start.
set JRUBY_HOME= your_installed_jruby
set PATH= %PATH%;%JRUBY_HOME%\bin
http://jruby.org/download
I don't know what the windows installer does, but I would think it would do something similar.

Emulate terminal to run ruby code on browser

I want to create a unix terminal on my RoR website. On this terminal, the user should be able to execute ruby code. I have no idea how can I create such terminal on browser.
Can anyone guide me about required resources, available ruby gems and technology to be used?
Thanks a lot.
you can use backticks to run shell command
#resut= `#{cmd}`
or eval for ruby command
#resut= eval cmd
note: those are very dangerous to use in real world app

rails s command does not run from ssh

I am using Putty to connect to my localhost, and I don't have any problems apparently, however, when I run command rails s to start my rails 4.0.0 application from Putty, it gives me this message:
jose#jose-laptop:~/rails/dedicated-agenda$ rails s
The program 'rails' can be found in the following packages:
ruby-railties-3.2
ruby-railties-4.0
Try: sudo apt-get install
I don't get that message from the terminal though, the application starts running just fine.
I had to reinstall ubuntu so I upgraded to ubuntu 14.04 just in case you need to know.
I don't know if I am missing something in my ssh settings or how could I use rails s from Putty.
Thanks in advance.
Your PATH environment variable is set differently when you are executing programs in an interactive shell and by ssh(using putty).
Use absolute path of the program to not depend on the PATH variable.
You can also set the right PATH variable at ~/.profile file and load the updated variables using the command source ~/.profile.
Now, you should be able to run the command.
You can use the command
>which rails
to see where rails is installed on your working session.
Then you need to make sure that is in your path when you ssh in.
If you are ssh'ing in as a different use then that user may not have permission to see the rails executable.

Resources