Error while typing rails server command - ruby-on-rails

I am a beginner.I am trying to launch rails server using the command.But I am getting an error. I tried searching in the google but no results.I will attach a picture of the log I got when I executed that command.

What Mysql error are you getting?
And what is in your config/database.yml?
So far, you have created the folder structure for a Rails app (rails new), then installed all the relevant components (bundle install).
When you start the server (rails server) it starts in "development" mode (you also have "test" mode for unit tests and "production" mode for when your app is live - and each has slightly different options). One of the first things the Rails server tries to do is connect to the database, so it looks in config/database.yml for the database specified in the development section.
So probably, it's trying to connect to a database that doesn't exist yet, with a username and password that are wrong.
First thing to do is to update the username and password in config/database.yml to match your local Mysql server.
Second thing to do is to build the development database; the command for that is "bin/rake db:create" (or "bundle exec rake db:create" if you're on Rails 3.x).
Hopefully that should be enough to get your server started.

How did you setup your rails app?
It seems like maybe you didn't type
bundle install
This command downloads and updates all of your gems. Action View is a rails dependency.

Related

Ruby On Rails - "OpenSSL::Cipher::CipherError at ..." when connecting to imported database

I am running my Ruby On Rails 5 app on localhost and now, I imported the production database. It's a PostgreSQL database, exported via the pg_dump tool.
When I modified the database.yml file the Rails app and set there the newly created database, I got this error when running the Rails app (tried also to change the port on which the app is running, but it didn't help):
OpenSSL::Cipher::CipherError at /
In the Rails console is not any information about the error.
What is the reason of such an error? I tried to export the database from the staging server and use it on localhost and everything worked fine.
Based on the docs: https://ruby-doc.org/stdlib-2.4.1/libdoc/openssl/rdoc/OpenSSL/Cipher.html#method-i-final
It looks like you don't have the proper encryption key to connect to the production db.
I would guess you either do have the proper one for staging, or staging is running unencrypted.
Here's someone else with the same error caused by an incorrect key: OpenSSL::Cipher::CipherError when running staging DB on local

Rails server not working when copying back from staging deploy

I ssh copied a rails app from a staging server because the development repository has been lost. My goal is to create a new development code base using the deployed code as a source. So far I have removed a hidden .bundle folder and replaces several aliases with folders and files. I then ran bundle install. For the database I did a sql dump from staging and used it to build a development database. I think I'm ready to run rails server. But when I try to run rails server in the base directory. It gives me the rails command line help as if I was running rails s in a directory with no app.
I'm not even sure if it is possible to reverse a deploy this way. I've looked at the rails guide on the app initialization process and all the files seem to be in place.
Remember that for Rails to start up, you need bin/rails, bin/bundle, config/boot, etc.
If you restore those files, it should work again.

Rails 3.2.6: Deleting all data and public files from production server

I have successfully managed to set up a VPS production server (Ubuntu 10.04 LTS) from the excellent Railscasts episode. It's an "internal" server (not live yet), so I've been building my app locally and doing a cap deploy at regular intervals to check that things are running smoothly.
However, what I'd like to do now is delete all records on the production server (as I've just been testing stuff) - that is, to start with a completely empty database for when the site actually goes live to the public.
Obviously, I can do this locally by running something like rake db:reset, but how do I do this on the production server? Should I be adding some code to my deploy.rb file?
I'm a bit of a noob at this, but I've been unable to find anything via a Google search.
** EDIT ** Oh, and obviously this is a one-off - once things go live, I'll remove any code which deletes records!
You can ssh into the server and run any rake command from the application directory. You could create a Capistrano task just to run this one rake task, but since this task is obscenely dangerous with any real system I would not recommend it. The last thing you would want is to accidentally run it.

How to tell when Rails app is activated by migration?

I'm trying to create a migration for my app, and in this app I'm using a gem that tries to startup a different service upon app startup. Apparently, creating a migration...
rails generate migration AddSomeStuffToTable stuff:string
...activates the app, and this gem which tries to connect to startup the service. It appears that starting up the app via generating a migration makes the service startup unable to connect, so it just keeps sleeping and trying again, never actually running the migration.
In this gem, I've already dealt with this for rake, so this is what I've got so far:
MyService.start unless defined? Rake or defined? IRB
This handles the rake problem (like rake db:migrate, rake db:populate), but how can I handle the creation of migration, which (as far as I know) is not a Rake task?
You could try using environment variables for disabling the service:
MyService.start unless ENV['NO_SERVICE']
And run your command like this:
NO_SERVICE=1 rails generate migration AddSomeStuffToTable stuff:string
However, I doubt this scales well, especially if multiple developers are in the app. A better approach might be to do the reverse of this, to only start the service if a particular env variable is present. However, going this direction, you'd need to make sure your app servers set this variable, for example:
Apache: SetEnv START_SERVICE 1
nginx: env START_SERVICE=1
thin: START_SERVICE=1 thin start

Failing to access environment variables within `database.yml` file

I have the following developement section of my development.yml file:
development:
adapter: postgresql
host: localhost
database: testtb
username: app_user
password: ENV['APP_USER_POSTGRES_PASSWORD'] <= Troublesome line
When I open a rails console via bundle exec rails console and type ENV['APP_USER_POSTGRES_PASSWORD'] I get back the DB password I've specified in my local profile. However, when I start my rails server, it can't connect to the DB, failing with
PGError FATAL: password authentication failed for user "app_user"
This was previously working when I had the DB password actually typed out in plain text, rather than trying to access it via ENV['...'], but for obvious reasons I want to keep the actual password out of this file entirely (and therefore out of the code repository) while still being able to commit other, non-secure changes to the database.yml file.
Is there something special about the syntax I'm missing, or are the environment variables for some reason not available when the database.yml file is being loaded?
Update: Some people report in the comments that this doesn't work as of Rails 4.2.x.x. I haven't tried it myself, so YMMV.
Ah, finally figured out the simple solution - it accepts embedded Ruby:
password: <%= ENV['APP_USER_POSTGRES_PASSWORD'] %>
Short and quick solution if you are running a Rails version > 4.2 Run the following command:
spring stop
..then run rails console or other rails command. My issue was that Spring server needed to be restarted in order to refresh/pickup my new ENV vars. I was starting up Rails console and it couldn't see them until I shut down Spring.
Previous versions of Rails didn't have this issue since they didn't use Spring server.
Another tool to help you troubleshoot -- Use the following command to print out your database.yml config. You can run it from the command line, but I prefer to run this within Rails console since then you can use awesome_print to make it pretty:
Within rails console:
puts ActiveRecord::Base.configurations
...or using awesome_print
ap ActiveRecord::Base.configurations
Or instead from the command line:
bin/rails runner 'puts ActiveRecord::Base.configurations'

Resources