Rails: Detecting User Agent Works in Development But Not Production

Rails: Detecting user agent works in development but not production?

Are you using Apache or nginx in front of your mongrel(s)?

Are you logging the user_agent? This is from my nginx.conf:

log_format main '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "http_x_forwarded_for"';

Rails code works in development but not in production

You're probably running different database types in development and production (i.e. MySQL in prod and SQLite in dev). Different databases handle Boolean values differently
e.g. 't' vs. 'true' vs. '1'.

I'll bet that if you compare your prod/dev logs you'd see the Boolean values are not being returned as 't' and 'f' in prod, causing your conditional to hit the else every time.

To solve this, make sure you're running the same database in both prod and dev!

Why Rails fails to detect AJAX request in production?

After burning long previous hours of frustration, I decided to debug the code it self that I was trying to create json object of. I passed an empty json object, and then, I got status 200 response with empty json object! So, the error ActionView::MissingTemplate was completely misleading!!, made me blind of the real error. I ran the code user.daip.class_rooms.as_json(:include => [:users]) in rails console, and I got this error:

Cannot have a has_many :through association 'ClassRoom#users' which goes through 'ClassRoom#class_rooms_users' before the through association is defined

Checked class_room model, and oh! I just had to flip those lines:

  has_many :users, through: :class_rooms_users
has_many :class_rooms_users

to:

  has_many :class_rooms_users 
has_many :users, through: :class_rooms_users

That was really sad story, the motto is, never trust error message when every thing seems to be fine, sad developers.

Web site: Only Allow iPod, Android, Blackberry, Windows Mobile

You want to pull out a request header called "User-Agent" and see if it matches any of the devices you've stated. If not then redirect to the appropriate page. Yes, this can be done with Ruby on Rails.

Unable to clear Rails cache and see an updated template

In this case, it's most likely not a caching problem.

In production mode, all classes etc. are preloaded during the startup of the app. Ergo you must restart the server when making changes to anything in your Rails app.

When you SSH to your server and type

$ ps auxf

You should be able to see the flags used when starting the app previously.



Related Topics



Leave a reply



Submit