RackEnv not respected when using route helpers from model - ruby-on-rails

Running Rails 3.2.13 on Passenger for Apache. My vhost:
<VirtualHost *:80>
ServerName server
DocumentRoot /srv/http
<Directory />
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
RackEnv test
RackBaseURI /rails_app
<Directory /srv/http/rails_app>
Options -MultiViews
</Directory>
</VirtualHost>
Works great. Only problem is that I'm using the route url helpers in a model, like so:
# egg.rb
def to_exhibit
return {
:edit_path => Rails.application.routes.url_helpers.edit_egg_path(self)
}
end
When rendering URLs in views, the sub-URI is used as I'd expect it to be, but when accessing the URL helper from within a model, it's discarded and the paths are always relative to the root.
From eggs_controller.rb:
edit_egg_path(1000) --> /rails_app/eggs/1000/edit
From some_model.rb:
edit_egg_path(1000) --> /eggs/1000/edit
Should this work as I expect it to? I don't mind manually fixing this at all, but I'm stumped as to where to find the value of the RackBaseURI so I can insert it manually, if it exists. I'd prefer not to manually define it again in some environment conf since Rails must know about it already.

Related

How to create dynamic subdomain rails

How can i create subdomain for each user that registers on my site? For example userone.mysite.com and usertwo.mysite.com.
In php it can be done using apache virtual host, but i can't figure out how to do the same in Ruby on Rails. Here is how it can be done in apache
<VirtualHost *:80>
ServerName www.mysite.com
ServerAlias mysite.com *.mysite.com
DocumentRoot /www/domain
</VirtualHost>
I went through many blogs but couldn't find the solution.
Please Advice.
<VirtualHost *:80>
ServerName mysite.com
ServerAlias *.my_site.com
# !!! Be sure to point DocumentRoot to 'public'!
DocumentRoot /var/www/html/my_site
<Directory /var/www/html/my_site>
# This relaxes Apache security settings.
AllowOverride all
# MultiViews must be turned off.
Options -MultiViews
# Uncomment this if you're on Apache >= 2.4:
Require all granted
</Directory>
</VirtualHost>
and do not forgot change cname * entry to your domain in DNS
Addition to Dinesh Saini answer - you also need to update Rails configs accordingly. For example, if you need deep subdomains you should change
config.action_dispatch.tld_length in staging.rb and you should re-check routes.rb.
Live example: I had to implement shop displaying by subdomain - example URL
my-shop.shop.testapp.com.So what I did except server config changes
constraints (lambda { |req| req.subdomains[1] == 'shop' }) do
get '/', to: 'shopes#show', as: :shop
end
In controller to find resource
Shop.find_by(id: request.subdomains[0])
I also set
config.action_dispatch.tld_length = 2
I did this for staging env because it has such URL staging.testapp.com so I need another subdomain level. I think for you it's only good to check that it's has 1.

Running Rails+Passenger+Devise from a subdirectory?

I have a server A which proxies all traffic on /rails to server B.
So I setup this virtual host, and most things work...okay. link_to is broken and generates urls to /users as opposed to /rails/users, but I can work around that.
If I set config.action_controller.relative_url_root to /rails then my routes work okay EXCEPT all the devise routes. They point to the bare URL. How do I properly configure server B to understand that its running in a subdirectory and generate links and routes correctly?
<VirtualHost *:80>
ServerName http://ec2-url.compute-1.amazonaws.com/
SetEnv RDS_HOSTNAME "mydb..."
SetEnv RAILS_RELATIVE_URL_ROOT "/rails"
DocumentRoot /home/ubuntu/myapp/public
RailsEnv staging
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/app.log combined
PassengerLogLevel 3
<Directory "/home/ubuntu/myapp/public">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
Options -MultiViews
Require all granted
</Directory>
</VirtualHost>
I am using Rails 4.
In your environment files, add a config for OmniAuth.config.full_host.
OmniAuth.config.full_host = 'http://myfullurl/subdir'
Now, in application_controller.rb, add this method:
def after_sign_in_path_for(resource_or_scope)
path = super(resource_or_scope)
"#{OmniAuth.config.full_host}#{path}"
end
Would be great if you share your routes.rb, but I think the easy way to change devise routes is to put something like the following in routes.rb
scope '/rails' do
devise_for :users
end

In Server Images are not responding in rails3

I am having issue that with this alias when I am pointing to a server at that time images are not showing.
My configuration of server for the application is
Alias /questions /app/xyz/public
PassengerRuby /usr/local/rvm/wrappers/ruby-1.9.3-p327#abcd/ruby
#RailsBaseURI /beingme
RailsBaseURI /questions
<Directory /app/xyz/public>
RailsEnv production
Options Indexes FollowSymLinks -MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
Please provide me the solutions.
You have to define one more Alias for assets
Alias /assets /app/xyz/public/assets
It will work for you, I hope.

Seeing the document root instead of my rails app running, Ubuntu VPS with Phusion Passenger

I am running ubuntu on my VPS and have installed apache as well as phusion passenger to deploy my rails apps with. I went through the setup for phusion passenger and everything installed just fine. I edited the apache configuration files and have appended the information below. I have also made sure that the Virtualhost is not being overridden the default host and when I changed that and refreshed a browser I was brought to the public directory of my rails app instead of having the rails app being executed. Any help would be appreciated.
additions to /etc/apache/apache2.conf
LoadModule passenger_module /var/lib/gems/1.8/gems/passenger-3.0.8/ext/apache2/mod_passenger.so
PassengerRoot /var/lib/gems/1.8/gems/passenger-3.0.8
PassengerRuby /usr/bin/ruby1.8
PassengerDefaultUser www-data
<VirtualHost *:80>
ServerName localhost
DocumentRoot /home/bodylabel/globify/public
<Directory /home/bodylabel/globify/public>
Allow from all
Options -MultiViews
</Directory>
</VirtualHost>
/etc/apache2/sites-enabled/000-default
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
</VirtualHost>
/etc/host.conf
# The "order" line is only used by old versions of the C library.
order hosts,bind
multi on
Just ran sudo passenger-status and got back the following:
*** Cleaning stale folder /tmp/passenger.44591
*** Cleaning stale folder /tmp/passenger.44300
*** Cleaning stale folder /tmp/passenger.44372
*** Cleaning stale folder /tmp/passenger.44200
*** Cleaning stale folder /tmp/passenger.21157
*** Cleaning stale folder /tmp/passenger.43944
*** Cleaning stale folder /tmp/passenger.21589
*** Cleaning stale folder /tmp/passenger.44878
ERROR: Phusion Passenger doesn't seem to be running.
the obligatory dumb question, but, did you restart apache?
Also, although this probably isn't the source of your problem, you may want to use the debian-esque method of deploying sites here and drop that virtualhost config into /etc/apache2/sites-available and run a2ensite to activate it.
There's probably a similar construct in place for modules-available and modules-enabled, where you might put that passenger code, rather than in apache2.conf.
You have two VirtualHost with the ip/port name *:80. Apache will do one of two things here. If you have a matching NameVirtualHost directive in your config then it will look for ServerName (and ServerAlias) directives within those two virtual host sections, and if the incoming request matches those server names/aliases then it will serve that virtual host.
Otherwise, it will serve the first VirtualHost block that is defined first in your configuration files. So, which one is being served will depend on the order that sites-enabled/000-default is included relative to the location of the VirtualHost section in apache2.conf

Setting up Rails app on Apache with passenger - Rails doesn't seem to load

I am trying to set up Rails on a Ubuntu instance by following along with documentation such as https://help.ubuntu.com/community/RubyOnRails. I am trying to set up the app from the Rails Getting Started page here: http://guides.rubyonrails.org/getting_started.html.
What I have done so far:
Installation of Ruby/RubyGems/Rails through RVM and GEM (this installed Ruby 1.9.2 and Rails 3.0.7)
Installation of the mysql and mysql2 module (latter which didn't work)
Creation of rails app (blog app as tutorial is instructing)
Sym linking of /home/me/www/blog/public/ from /var/www/blog/
Creation of DB through rake
Installation of passenger module for Rails execution in Apache
Running of passenger-install-apache2-module to configure Apache
Deletion of public.html file from public/ rails app folder so that my controller/view can fire
Configuration of sites-available files for virtual hosts
Configuration of Rails routing
I believe it is the last two steps that are tripping me up, partly due to lack of familiarity with Apache and Rails (I am new to both).
In my virtual host file I am trying to point to the blog public dir in /var/www/blog. For my Rails route I have root :to => "home#index" as instructed.
When I browse to the http:// url I should normally be seeing my "Hello, Rails!" page per section 4.3 of the Getting Started guide, however all I see is the directory listing from Apache. Static pages work but not Rails processing.
At this point I am unclear if the passenger module is even doing anything or where to look for any evidence of what is happening. I tried various things like running the Rails app off a different virtual host called "blog" at http:///blog and the routing worked (static pages and all) but no Rails as above.
Update: after playing around some more, I now get a new error message (404):
Not Found
The requested URL /dispatch.cgi was
not found on this server.
I have since determined that this cgi 404 was caused by a rewrite rule I had added to .htaccess from guidance from the help.ubuntu.com page I pointed out above. Looks like it wasn't necessary or applied to an older version of Rails. The inconsistencies in instructions when looking up help online is disorienting! Removing the file leaves me with the previous problem though.
Here's the entry in the Apache log that I believe correlates with the first error. Although I am no longer getting this error it seems, may be related to something else I was trying before...
[Fri May 27 22:49:41 2011] [notice] Apache/2.2.16 (Ubuntu) PHP/5.3.3-1ubuntu9 with Suhosin-Patch Phusion_Passenger/3.0.7 configured -- resuming normal operations
[Fri May 27 22:51:18 2011] [error] [client 192.168.1.141] File does not exist: /var/www/blog/rails, referer: http://192.168.1.138/blog/
Here's the most recent entry with the 404 on the cgi thing:
[Sat May 28 08:01:18 2011] [error] [client 192.168.1.141] File does not exist: /var/www/blog/dispatch.cgi
What could I be missing? Thanks!
Additional details as requested. Apache sites file:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/blog/
RailsEnv development
# <Directory />
# Options FollowSymLinks
# AllowOverride None
# </Directory>
<Directory "/var/www/blog/">
Options Indexes FollowSymLinks -MultiViews +ExecCGI
AllowOverride All
Order allow,deny
allow from all
# AddHandler cgi-script .cgi
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
</VirtualHost>
Another update: I was able to get it working, finally, by using a top level DocumentRoot (/var/www) and basing my Rails app off that using RailsBaseURI. This maps to the "Deploying to a sub URI" section of the Phusion Passenger instructions. It is used as http:///blog which is not really what I wanted to get, but it's kind of irrelevant as I am using this just to learn Rails in a virtual. I will need to go and work on getting it working as a top level app again sometime later. Here's my current (final) VirtualHost file for reference:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/
RailsEnv development
<Directory /var/www/>
Options FollowSymLinks
AllowOverride None
</Directory>
RailsBaseURI /blog
<Directory /var/www/blog/>
Options Indexes FollowSymLinks -MultiViews +ExecCGI
AllowOverride All
Order allow,deny
allow from all
# AddHandler cgi-script .cgi
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
</VirtualHost>
Deploying a Ruby on Rails application
Suppose you have a Ruby on Rails
application in /webapps/mycook, and
you own the domain www.mycook.com. You
can either deploy your application to
the virtual host’s root (i.e. the
application will be accessible from
the root URL, http://www.mycook.com/),
or in a sub URI (i.e. the application
will be accessible from a sub URL,
such as
http://www.mycook.com/railsapplication).
Deploying to a virtual host’s root
Add a virtual host entry to your
Apache configuration file. Make sure
that the following conditions are met:
The virtual host’s document root must
point to your Ruby on Rails
application’s public folder.
The Apache per-directory permissions
must allow access to this folder.
MultiViews must be disabled for this
folder.
For example:
<VirtualHost *:80>
ServerName www.mycook.com
DocumentRoot /webapps/mycook/public
<Directory /webapps/mycook/public>
Allow from all
Options -MultiViews
</Directory>
</VirtualHost>
You may also need to tweak your
file/folder permissions. Make sure
that the following folders are
readable and executable by Apache:
this public folder.
the application’s config folder.
all parent folders. That is, /webapps/mycook and /webapps must also
be readable and executable by Apache.
Then restart Apache. The application
has now been deployed.
Read the whole docs here
Double check to make sure passenger is loading your application by typing
sudo passenger-status
This will show you an error if passenger isn't loaded or the below if it is. Note the part where it shows you which application or applications it has loaded and what the current uptime is:
----------- General information -----------
max = 6
count = 1
active = 0
inactive = 1
Waiting on global queue: 0
----------- Application groups -----------
/home/rourkem/public_html/evecord.com/current:
App root: /home/rourkem/public_html/evecord.com/current
* PID: 18976 Sessions: 0 Processed: 5 Uptime: 23m 55s
Be sure to delete the file index.html from the public directory of your application.

Resources