How do I set rails to use different paths? - ruby-on-rails

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

Related

How to remove # from Angular App?

I have an Angular Application (Angular 5) as well as Ruby on rails backend. I put my angular app in the ruby-on-rails public folder and its working fine with a '#' prefix.
Now, I want to remove that '#' Prefix. When I remove it, I am getting errors.
I've deploy ruby-on-rails application on EC2 with Apache2 and Passenger.
This is my PATH for .htaccess file
/var/www/ro-blog/.htaccess
Can anyone please help me - How can I fix that?

Change the root directory of rails application

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'

How to serve an api subdomain using apache, passenger, and rails?

I created a subdomain like the following myapp.example.com and it works great ...has been. Now I want to build an API using rails for the app (rails itself) so I created the subdomain api.myapp.example.com. Currently the problem I'm getting is the api subdomain isn't getting routed to the rails app.
I tried adding the following to the apache conf file in /etc/apache2/sites-enabled/example.com.conf
# example.com.conf
ServerAlias *.myapp.example.com
I then restarted Apache, but still api.myapp.example.com wasn't being routed to rails application.
Ahhh, it ended being an easy fix
# example.com.conf
ServerAlias myapp.example.com api.myapp.example.com

How to deploy Rails 3.1 app in a subdirectory

How do I configure a Rails 3.1 application to run under a specific directory such as "/r"?
I tried in config.ru:
map '/r' do
run Debtor::Application
end
but that just returned "Not Found: /r"
To get it to work I had to enclose all routes in a scope:
scope '/r' do
#routes
end
and to add the following line to config/applcation.rb
config.assets.prefix = "/r/assets"
and to move my jquery ui css files from /stylesheets to /r/stylesheets.
this seems too complicated. isn't there an easier way? and why isn't my config.ru setting working?
my use case is to have a rails powered ajax backend for a wordpress server.
are you running under passenger?
Then RailsBaseURI is probably what you want.
https://www.phusionpassenger.com/library/deploy/apache/deploy/ruby/#deploying-an-app-to-a-sub-uri
If not running under passenger, please update your question to show what you are deployed under.
What worked for me was creating the symbolic link for the sub-uri (/info) to the 'public' folder of the application (setup under another user on my server, /home/otheruser/current/public).
ln -s /home/myapp/current/public /home/mysite/public_html/info
Then I inserted this configuration inside of the VirtualHost entry for the site:
Alias /info /home/myapp/current/public
<Location /info>
PassengerAppRoot /home/myapp/current
RackEnv production
RackBaseURI /info
</Location>
No scoped routes, no asset prefix configuration.
Here’s how to deploy a Rails 3.1 app to a subdirectory in Apache, replacing config.action_controller.relative_url_root which no longer exists.
In config/routes.rb:
scope 'my_subdir' do
# all resources and routes go here
end
In your Apache configuration file:
Alias /my_subdir /var/www/my_subdir/public
<Location /my_subdir>
SetEnv RAILS_RELATIVE_URL_ROOT "/my_subdir"
PassengerAppRoot /var/www/my_subdir
</Location>
And it should work, including automatically pointing all your assets to /my_subdir.

Changing the base URL for Rails 3 development

I know I'm going to deploy to an environment with my application running with a base URL which looks like this:
http://someserver/mydepartment/myapp
My development environment is set up to use the default Rails configuration, which looks like this:
http://localhost:3000/myapp
I'd like to model this deployment path in my development environment. That is, I'd like to develop with a base URL which looks like this:
http://localhost:3000/mydepartment/myapp
That way, I can make all my URLs relative to "/" and they will work in both environments.
How can I change it so my application will live at this path in my development environment?
Solutions I've found, but don't work for me:
Setting the scope in routes.rb doesn't seem to work for the static content in public.
Using Apache's rewriting capabilities. I don't want to install Apache on my development box. Ideally the solution would work with WEbrick, though I seem to have Mongrel mostly working as well (there are some problems with Mongrel and Ruby 1.9.2).
Setting relative_url_root and similar suggestions which don't work with Rails 3.
Dynamically generating CSS/JavaScript and adjusting the paths to compensate between development and production environments.
You can try mapping your rails app rack config to a different base_uri.
All you need to do is wrap the existing 'run' command in a map block
try doing this in your rails 'config.ru' file:
map '/mydepartment' do
run Myapp::Application
end
Now when you 'rails server' the app should be at localhost:3000/mydepartment .
Not sure if this will give you the desired outcome, but worth a try.
Here’s how you can deploy a Rails 3.1 app to a subdirectory in Apache, replacing config.action_controller.relative_url_root which no longer exists.
In config/routes.rb:
scope 'my_subdir' do
# all resources and routes go here
end
In your Apache configuration file:
Alias /my_subdir /var/www/my_subdir/public
<Location /my_subdir>
SetEnv RAILS_RELATIVE_URL_ROOT "/my_subdir"
PassengerAppRoot /var/www/my_subdir
</Location>
And it should work, including automatically pointing all your assets to /my_subdir.

Resources