bundle exec not working in Windows - ruby-on-rails

I am following the Redmine Install Tutorial
on step 5 it says
Step 5 - Session store secret generation
This step generates a random key used by Rails to encode cookies storing session data thus preventing their tampering.
Generating a new secret token invalidates all existing sessions after restart.
bundle exec rake generate_secret_token
I'm on Windows 10. When I try to do bundle exec in CMD, I get cannot find the path specified. For whatever reason I cannot find any reference to this problem (perhaps I'm not searching for the right terms). I can't imagine I'm the first to experience this but in any case, how can I resolve it? I can check ruby version, gem version, and rails version just fine. I've been able to do steps 1-4 fine so I don't get why I can't run bundle exec. I suppose this is implied but I can't do step 6 either.

Do you have rake rubygem installed? Try to run this in CMD:
gem install rake
Then run the command again:
bundle exec rake generate_secret_token
I'm looking at this: source

Related

Is there a way to connect to a Rails app's console on a linux server?

I have deployed a rails application using capistrano on a linux server and it is running without any problems. When I connect to my remote server via ssh, the folder structure of my app is quite different from what I have on my local machine. I would like to go to project root and say rails console so that I can have access to the console of my application. Is there a way to achieve this?
When I go to ~MyApp/ folder and run rails console it says command rails not found. I think that is probably because the app is running in another folder.
bundle exec to the rescue inside of project folded:
$ RAILS_ENV=production bundle exec rails console
If you've installed with rbenv it could be that you are defaulting to the wrong Ruby version. So you can run rbenv exec to get it to run on the right version, along with bundle exec to run the right version of Rails
So try running this:
rbenv exec bundle exec rails console
To sum up for anyone having the same problem in the future,
I went to MyApp/releases/2020331231231231 which is the latest release of my app and there I used this command RAILS_ENV=production bundle exec rails console and I was able to do whatever I wanted to do in the console.
First you need to move inside the folder where the code is.
Check Capistrano config if deploy_to is defined. (If it's not it should be set to /var/www/#{application}/ by default read here)
Since Capistrano keep older versions of your app go to the current folder which is a symlink of the latest deployed version.
And run ENVIRONMENT=production bundle exec rails c or ENVIRONMENT=production bin/rails c. If doesn't work try with rbenv ENVIRONMENT=production rbenv exec bundle exec rails c

use of bundle exec in Ruby on Rails

What does bundle exec rake db:migrate really means? or bundle exec rails s
I understand that bundle handle all the gems we specify in our Gemfile,and rake handle all the things which you can do through scripts but these commands don't get executed without prefixing bundle exec and I couldn't find any documentation about this on internet !!
Bundler - The best way to manage your application's dependencies
Run the command in context of the bundle
$ bundle exec
Exec runs a command, providing it access to the gems in the bundle. While using bundle exec you can require and call the bundled gems as if they were installed into the systemwide RubyGems repository
With:
bundle exec rake db:migrate
In an informal way, it means that your rails environment will
-Install dependencies with bundler
then
-Run your database migrations in your rails app (/yourapp/db/migrate)
Instead of a database operation, you could do any other command line stuff that can be controlled by rake (like building your app, extensions, cleaning some files, etc.)
This can be useful if you want to avoid confusing scenarios where maybe some gems in your setup are outdated, or missing from your environment. It's a good practice because calling rake by itself will not guarantee that you got all your dependencies in check.
Bundler acts to create a somewhat isolated Ruby language programming environment separate from the underlying host system. There are limits to what it can do. For example, you need something like RVM to manage different versions of the Ruby language virtual machine. And of course both Ruby and Bundler itself have to be available outside your project in order to run.
However, within a single project bundle exec will restrict access to gems installed by Bundler into that project. This means that the Rake utility when invoked via bundle exec will be the version of rake installed in your project and not the system-wide version. It also means that `bundle rake db:migrate' will use the correct version of rails installed for your project. This isolates your project from updates done to the Ruby environment on the host and thereby prevents compatibility issues from arising.
You can get around the verbosity by creating aliases in your .bash_profile (assuming you are using OSX or Linux/unix) for bundle exec, bundle exec rake and bundle exec rails. I use bexec, brake and brails.

Piping email from Postfix to Ruby task error

I'm trying to pipe mail from postfix to a rails tasks. I've got this working successfully when just piping to a vanilla ruby script, but when switching to a rails task I get the following error in the mail.log from Postfix:
(Command died with status 1: "/usr/local/bin/rake". Command output: /usr/lib/ruby/1.9.1/rubygems/dependency.rb:247:in to_specs': Could not find rake (>= 0) amongst [] (Gem::LoadError) from /usr/lib/ruby/1.9.1/rubygems/dependency.rb:256:into_spec' from /usr/lib/ruby/1.9.1/rubygems.rb:1231:in gem' from /usr/local/bin/rake:22:in' )
Here is the piping line from master.cf
email-task unix - n n - 2 pipe
flags=Xhq user=appuser directory=/home/myapp/application/ argv=/usr/local/bin/rake RAILS_ENV=production myapp:process_email
Running the command which rake returns /usr/local/bin/rake so I have verified I have the correct rake path.
I've tried setting ENV['PATH'] inside of the .rake file to /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games and I've also tried cd'ing to the rails app directory in the pipe command but still causes the same error. I've done bundle install and update etc.
Any help is greatly appreciated...thank you!
UPDATE
I tried using bundle exec rake etc as suggested but am now getting this error:
Command died with status 127: "/usr/local/bin/bundle". Command output: bundler: command not found: rake Install missing gem executables with bundle install
I've tried adding gem 'rake' to the Gemfile as well. I'm assuming bundler can't find the Gemfile?
UPDATE (FIXED)
So the command that finally worked was the following:
email-task unix - n n - 2 pipe
flags=Xhq user=appuser directory=/home/myapp/application/ argv=/usr/local/bin/bundle exec /usr/local/bin/rake RAILS_ENV=production mytask
It was a combination of errors on my part.
Full paths need to be used for bundle and rake
app name is not needed in the task param for rake since the directory is being selected before executing
A side issue: an old version of ruby was being used which caused an unrelated gem error after executing the postfix pipe
Appreciate the help everyone.
I think your on the wrong track, it's not the system that can't find rake it's your rubygems. Instead of running rake as your command, try running bundle passing whatever you need to in order for it to basically be bundle exec rake...etc
Update
Based on your new information, it would seem that the rake gem is not installed in your app. Run bundle show rake
Assuming it's not there, I'm wondering why. Did you ship your app with packaged gems or something? Basically, did you not run bundle install in your deployed app?

Use bundle exec rake or just rake?

I learned Rails using just the rake command like rake db:migrate; however, I read that I should be using the bundle exec rake ... instead of just plain rake. Now I'm confused about which to use.
Should I be using bundle exec rake instead of just plain rake or is it just a preference thing? Any insight would be much appreciated! Thanks!
bundle exec executes a command in the context of your bundle.
That means it uses the gems specified in your Gemfile. Much of the time, running bundle exec rake foo has the same results as if you just ran rake foo, especially if you have the same gems installed systemwide as in your Gemfile. However, some applications may specify different versions of gems than the ones you have installed systemwide, and may want those exact gems and versions to be able to run correctly. If you just run without bundle exec, you may get some weird errors.
Using bundle exec guarantees that the program is run with the environment specified in the gemfile, which hopefully means it is the environment that the creators of the program want it to be run in, which hopefully means it should run correctly no matter what weird setup you have on your computer.
It basically standardizes the environment under which the program is run. This helps avoid version hell and makes life much easier.
See http://bundler.io/v1.3/man/bundle-exec.1.html for more info.
$ bundle exec rake db:migrate
Uses the version of rake specified in the Gemfile to execute the rake task db:migrate.
But there is no rake gem specified in the Gemfile!
Yes, but a rake gem was installed as a dependency of some other gem--look in Gemfile.lock. So the rule must be: Uses the version of rake specified in Gemfile.lock.
But Gemfile.lock doesn't specify a specific version--it specifies a version greater than x.y!
Then the rule must be: Uses the version of rake that was installed in the current gemset.
$ rake db:migrate
Normally, when you issue a command on the command line, e.g. rake, your system searches for the command in the list of directories specified in your PATH environment variable. The first directory that contains the command is the version of the command that is used. To see which directory that is, you can do:
$ which rake
So if you execute,
$ rake db:migrate
that may use a different rake gem than the one you installed with bundle install. But, even if your system finds the same rake version as bundle exec, any gems required by the rake source code will be searched for in places outside your project's gemset. Therefore, there are many ways that just:
$ rake db:migrate
can screw things up.
According to the Ruby on Rails Tutorial Book(free online), section 3.6, if you are using rvm 1.11.x+ then you do not need to preface commands with bundle exec.
running any exacutable without bundle exec will have Rubygems fetching the latest version of the gem installed in your system.
By adding the bundle exec prefix instead will have the executable running in the context of your Gemfile.lock, which means that will be run using the version defined in the gem file.

Rails 3 Sandbox Console

In Rails 2 you're able to run
script/console --sandbox
so you can play with production data and not accidentally break anything.
I can't seem to find the equivalent command for Rails 3. Does anyone know what it is?
Easy, type in:
bundle exec rails c -s
and that is it.
$ bundle exec rails c --help
Usage: console [environment] [options]
-s, --sandbox Rollback database modifications on exit.
--debugger Enable ruby-debugging for the console.
--irb DEPRECATED: Invoke `/your/choice/of/ruby script/rails console` instead
It is simple, but, sometimes, if you are not running rails executable using bundle exec, it may, or may not, result in an error. In order to avoid this, ALWAYS use bundle exec.
To quote bundler page (if not documentation):
In some cases, running executables without bundle exec may work, if
the executable happens to be installed in your system and does not
pull in any gems that conflict with your bundle.
However, this is unreliable and is the source of considerable pain.
Even if it looks like it works, it may not work in the future or on
another machine.

Resources