Change the root directory of rails application - ruby-on-rails

My server is on http://digitalocean.com
Currently my Rails directory is pointed to /home/rails/rails_projects/
but I need the default directory to be /home/rails/newProject/
I also installed rails 4.2.2 and made it default version.
How would I do that?

In order to change Rails.root to a different directory you can give it a relative path. In your case it would be:
Rails.application.config.root = "../newProject"

In Rails 3 or Rails 4:
Rails.root
which returns a Pathname object. If you want a string you have to add .to_s. If you want another path in your Rails app, you can use join like this:
Rails.root.join('app', 'assets', 'images', 'logo.png')

If it is digital ocean server and you know only this much than it may be deployed with nginx. For that you should go to
/etc/nginx/nginx.conf
file and there you would see line like this
root /home/rails/rails_projects/
change it to
root /home/rails/newProject/
similar steps would go for apache.

When to change the root so that the rails application redirects to your path, you have to configure the routes.rb present in config.
It should be :
root 'controller_name#method_name'

Related

Issue Deploying Rails App to Heroku JWT auth.rb located in the Lib folder?

I'm trying to host an Rails API on Heroku and I'm having an issue when I sign up a user. Say's there's an error on Line 29. Line 29 is
jwt = Auth.encrypt({ user_id: #user_id })
Which makes a call to my Auth class, which isn't located in my project/app folder at all, but located outside in my project/lib/auth.rb. Could that be the issue?
If this works in dev, then yes, the issue is probably that the file is located under lib and Rails hasn't been configured to autoload files from that path.
You can either get rails to autoload files within lib by setting the following in your application.rb file:
config.autoload_paths << Rails.root.join('lib')
Or, alternatively you can add require 'auth' in your user_controller.rb.

Changing Directory Names in a Rails App

I'm relatively new to Rails as well as using PassengerPhusion. I am running an Ubuntu server on Azure, and have the demo app that Passenger provides working fine. I've even changed the text on the homepage.
My question is this:
In my directory, the file directory's name for the app is passenger-ruby-rails-demo and while I am experimenting, i am changing the name of the directory to something like passenger-ruby-rails-demo-test and it returns an error message when viewing fleetpro.cloudapp.net.
I've tried looking through files trying to figure out how this is routed but haven't had any luck. Is there a file within the Rails installation that is telling Passenger to be inside the specific passenger-ruby-rails-demo directory? Pretty newbish question, but it is really bothering me!
I'm not sure about how the naming convention works in regards to the root directory name of your app "passenger-ruby-rails-demo", but I believe the name of that directory is important to running your Rails app, and might have to do something with the name of the module in your config/application.rb file which is named after your Rails app.
There is a solution though: use gem rename.
Add gem rename to your Gemfile and run bundle install.
Then in your app's root directory, run this:
rails g rename:app_to New-Name
This will basically "clone" the app with your new name. You may have to check to make sure all your config files are present afterwards, but from my experience using it, it was a quick breeze. You will most definitely have to push the new renamed app back to git or Azure.
EDIT
As an example I renamed a Rails app to show what you could expect from the output after running the command:
The Rails app's name isn't the problem, it's the PassengerAppRoot switch you'll be using:
PassengerAppRoot /path/to/your/app
Rails doesn't actually care which folder it's put into, so renaming Rails won't fix your problem.
Renaming Rails only changes the internal class references within your application (things like Rails.application.name which have very subtle implications for your app).
In your Azure server, you'll need to locate either your .htaccess / httpd.conf / nginx.conf file, and change the PassengerAppRoot to your new path. This should resolve the issue.

"No such file or directory - /public/settings.xml" in Rails when the file is there

As you can see, the file is there. I made sure to switch rights to that file to everyone - anythiig I am missing that is obvious?
Ruby 1.8.7
Rails 3.0.3
You shouldn't be using / before public, as it is searching for public directory in root , so for example, if you wanted to remove the file from a terminal you would say:
rm public/settings.xml
Rails serves up files in the public directory from the root of the application. So instead of using /public/settings.xml as the path in the browser, you should just use /settings.xml

How do I set rails to use different paths?

I am using Rails 3 and I am proxying my app through nginx to a subdirectory, such as /pdfs/, now the stylesheets are linking from /assets/, How can I link them from /pdfs/assets/?
Try putting config.action_controller.relative_url_root = "/pdf" in environments.rb and start your server normally.
Then Rails will prepend /pdf to all your routes

How can I run my Rails application in a non-root context?

I'm working in a non-privileged environment, and my Rails application's root url is http://foo.com/bar.
What is the simplest way to tell Rails that, for example, my stylesheets are in /bar/stylesheets, not /stylesheets, and make model_url point to /bar/model/baz instead of /model/baz?
Regardless of the way you are running the application to that specific path (be it script/server --path=/sub-uri or have deployed passenger to a directory) you don't need to change the code as the url helpers will automatically adjust their path to the environment.

Resources