Deploying rails 3.1 app in subdirectory - ruby-on-rails-3

I'm trying to deploy my rails 3.1.3 app in a subdirectory on the server:
Apache (reverse proxy) => unicorn (listening on localhost:5000)
The problem is with url_helper.
via the --path switch in unicorn I'm setting my /subdir
It gets picked up by rails; DashboardController.config.relative_url_root gives me that subdir.
But when I use something like 'members_path' in my view it gives me '/members' and not '/subdir/members'
what am I missing?

I had exactly the same problem. Two things were missing/faulty in my setup.
My web-server had an superflous, faiulty rewrite-rule, rewriting /prefix/<controller> to just /controller.
It seems Rails3 routing are by default ignoring relative_url_root. (Stupid IMHO) There's a good tip in https://stackoverflow.com/a/5457484/103192 that shows a trick to make it work again.
Otherwise, you can wrap the run statement in your config.ru with the
following block:
map ActionController::Base.config.relative_url_root || "/" do
run FooApp::Application
end
I now have it working much like one would want it to work.

Related

how to properly see the errors of a flask application in production mode

I have made a flask application at my local computer in the debugging mode and it runs all fine. But when it comes to production, the website gives me 500 or internal server error, which I have no idea what the bug is. I am fairly new to flask production and this has been stopping me from moving forward for quite a few days.
My questions are:
1> in my local development environment, one could always print things out. But how can I see those prints in the production stage?
2> Do I see them through Apache2 log? Where is Apache2 log?
For production, I actually followed the tutorials from pythonprogramming.net. Youtube link is here:
https://www.youtube.com/watch?v=qZNL4Ku1UQg&list=PLQVvvaa0QuDc_owjTbIY4rbgXOFkUYOUB&index=2
To use a very simple example, if the code imports a package which wasn't installed, where can we see the errors?
Thanks in advance.
I've tried to use to use try ... except block for every flask function. Whenever there is an exception, it can be return to the front-end. But what about other errors?
I found out:
Use logging module
Read apache2 log from /var/log/apache2

Heroku mongo_mapper MONGOHQ_URL unparseable by ruby uri

I have scoured the Internet without finding an adequate solution, so I'm here to ask the SO community for help.
I'm migrating a rails 2 app on Heroku Bamboo to Cedar. Concomitantly, I am moving to rails 3 and ruby 2.0.0 (though I get this error with ruby 1.9.3 as well).
It seems that MongoMapper has undergone a bit a change from 0.8.6, to the point where I can't use our old initializer anymore. I'm attempting to use the latest 0.13.1 version of mongo_mapper.
According to https://devcenter.heroku.com/articles/mongohq, I just need to set config/mongo.yml to:
production:
uri: <%= ENV['MONGOHQ_URL'] %>
and I'm all set (I also commented out the old initializer code). However, it fails with the following connection error:
Feb 05 11:18:28 my-app-name app/web.1: /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/uri/generic.rb:214:in `initialize': the scheme mongodb does not accept registry part: heroku:2QMdYzo6z5nmJJsmWAWKd_205umc05tBuP2ZodGaNDZ7N5rE4ns09jhsfFBjmkQ2ls_rfTiVC0lD24Y2byDXbg#candidate.21.mongolayer.com:10499,candidate.4.mongolayer.com:10625 (or bad hostname?) (URI::InvalidURIError)
An invalid URI is understandable, but that's what being fed to me by Heroku. What do I am missing in order to for the uri parser to accept it?
Thank you in advance.
The answer lies in the presence of that comma (,) in the uri. When I removed the second host and the comma, it behaved correctly :-)
Specifically, I created a Heroku ENV variable called MONGOHQ_URL2 without the second host name, then updated my config/mongo.yml to use that one instead.

A custom route is matched in development but results in 404, in production

I have a following definition in my routes.rb
match "/block/:name/:action" => proc { |env|
#heavy magic happens here
}
I use it for handling rendering of cells. My problem is that the following link:
/block/reporting%2Fother%2Fexample/new?exampleable_id=23736&exampleable_type=Abc%3A%3ASomeType
works perfectly fine in development mode, but in production I get "404 Not Found". The only difference between production and development urls is that the one in development uses http and the one in production uses https. I would understand if I wrote a faulty code my proc block, but for the love of god I can't understand why it is not matched in production environment, when it works perfectly fine in development.
Any hint on how to debug this is highly appreciated. There is not so much hair left on my head.
I got this error even when if I finally launched the project in production environment on my local machine. The thing is we do not use Thin in production. We use passenger. After I finally got passenger working locally, I managed to recreate the error.
It turned out that with passenger, the :name in aforementioned route wasn't interpreted as "reporting%2Fother%2Fexample", but possibly as /block/reporting/other/example/new" with the rest not matched, of course.
I fixed the error by replacing
match "/block/:name/:action"
to
match "/block/*name/:action"
I hope this answer to my own question helps someone in the future.

puma hot restarting on_restart function: necessary for Rails apps?

In the configuration file example for Puma, it says the following for the on_restart function:
Code to run before doing a restart. This code should close log files,
database connections, etc.
Do I need to implement this for a Rails app, to close connections to the db and the logfile, or is that taken care of automatically? If not, how do I actually do all that?
No you don't, Rails takes care of reloading your code automatically. But this code reloading support is limited. For example changes to application.rb are not applied until you restart the app server.
But I would recommend Phusion Passenger over Puma. Phusion Passenger is a lot easier to setup, especially when you hit production. Phusion Passenger integrates into Apache and Nginx directly and provides advanced features like dynamic worker management. Phusion Passenger is very mature, stable and performant and used by the likes of New York Times, Symantec, AirBnB, etc.
I've found that using Redis as my Rails.cache provider causes an error page upon the first request every time my Rails/Puma server is restarted. The error I got was:
Redis::InheritedError (Tried to use a connection from a child process
without reconnecting. You need to reconnect to Redis after forking.)
To get around this error, I didn't add anything to on_restart, but did have to add code to on_worker_boot ( I am running Puma with workers=4 ):
puma-config.rb
on_worker_boot do
puts "Reconnecting Rails.cache"
Rails.cache.reconnect
end

ruby on Rails 3 and routing

I am new to rails and have been working thru a number of tutorials. I have a controller with the following actions: Contact and Home. The routes.rb has the following:
SampleApp::Application.routes.draw do
get "pages/home"
get "pages/contact"
# The priority is based upon order of creation:
# first created -> highest priority.
Everything else in the standard routes is commented
When I try to reach the page http://localhost:3000/pages/home
I get
Routing Error
No route matches "/pages/home"
on the web page.
I restarted the rails server and checked routes with rake routes - results below
pages_home GET /pages/home(.:format) {:controller=>"pages", :action=>"home"}
pages_contact GET /pages/contact(.:format) {:controller=>"pages", :action=>"contact"}
I tried other tutorials that were based on 2.3 version of rails and had lots of routing issues but was running 3.0 instead so went to 3.0 tutorial. Same routing issues. I even have the same problem when I put a static page "hello world" in the public folder. According to tutorial, that should just come up but I get same routing error message.
Any suggestions would be most appreciated.
I solved my problem. A real noob mistake. Running rails, it is important to be in the right directory. I was in a previous example directory so the routes I needed were not there.