Rails Routes Different In Development and Production - ruby-on-rails

I have a route working just fine in my development environment, but for some reason it's not working in production and I can't figure out why.
When I go to http://localhost:3000/api/v1/register_panelist?api_key=ff4a6fa1c975693bedc2122e6943946b in my development environment, it works great.
However, when I go to http://example.com/api/v1/register_panelist?api_key=ff4a6fa1c975693bedc2122e6943946b it does not resolve, and ends up falling to a "page not found" which is the catch all route at the bottom of my routes file.
In my routes.rb file I have this:
constraints(ApiConstraint) do
namespace :api, defaults: {format: 'json'} do
namespace :v1, defaults: {format: 'json'} do
match "register_panelist", to: "appusers#register_panelist", via: 'get'
match "get_surveys", to: "appusers#get_surveys", via: 'get'
end
end
match "api/v1/*path", to: "api/v1/misc#api_not_found_404", via: :all, format: 'json'
end
...
#at the very bottom
match "*path", to: "static_pages#not_found_404", via: :all, format: false #, :constraints => {:format => [:html, :png]}
In my development environment it seems to resolve correctly. But in production it seems to be falling through to the bottom of the routes.rb file.
Any ideas why?
EDIT: Adding Logs:
Production:
production.log — I, [2017-02-12T11:55:31.455331 #27545] INFO -- : Started GET "/api/v1/register_panelist.json?api_key=ff4a6fa1c975693bedc2122e6943946b&country_id=9&birthday_year=1989&birthday_month=2&birthday_day=16&gender=42198&hispanic=42200&zip=10022&state=45700&ethnicity=42215&standard_relationship=42232&standard_education=42243&standard_hhi_us=43511" for 75.100.38.224 at 2017-02-12 11:55:31 +0000
production.log — I, [2017-02-12T11:55:31.606946 #27545] INFO -- : Prod? true
production.log — I, [2017-02-12T11:55:31.607886 #27545] INFO -- : subdomain:
production.log — I, [2017-02-12T11:55:31.607948 #27545] INFO -- : protocol: https://
production.log — I, [2017-02-12T11:55:31.608020 #27545] INFO -- : Prod? true
production.log — I, [2017-02-12T11:55:31.608052 #27545] INFO -- : subdomain:
production.log — I, [2017-02-12T11:55:31.608086 #27545] INFO -- : protocol: https://
production.log — I, [2017-02-12T11:55:31.617812 #27545] INFO -- : Processing by StaticPagesController#not_found_404 as HTML
production.log — I, [2017-02-12T11:55:31.617925 #27545] INFO -- : Parameters: {"api_key"=>"ff4a6fa1c975693bedc2122e6943946b", "country_id"=>"9", "birthday_year"=>"1989", "birthday_month"=>"2", "birthday_day"=>"16", "gender"=>"42198", "hispanic"=>"42200", "zip"=>"10022", "state"=>"45700", "ethnicity"=>"42215", "standard_relationship"=>"42232", "standard_education"=>"42243", "standard_hhi_us"=>"43511", "path"=>"api/v1/register_panelist.json"}
production.log — I, [2017-02-12T11:55:31.636463 #27545] INFO -- : Rendered text template (0.2ms)
production.log — I, [2017-02-12T11:55:31.636883 #27545] INFO -- : Completed 404 Not Found in 19ms (Views: 11.4ms | ActiveRecord: 0.0ms)
Development:
Started GET "/api/v1/register_panelist?api_key=ff4a6fa1c975693bedc2122e6943946b&country_id=9&birthday_year=1989&birthday_month=2&birthday_day=16&gender=42198&hispanic=42200&zip=53593&state=45700&ethnicity=42215&standard_relationship=42232&standard_education=42243&standard_hhi_us=43511" for ::1 at 2017-02-12 05:56:46 -0600
Prod? false
subdomain:
protocol: http://
Processing by Api::V1::AppusersController#register_panelist as JSON
Parameters: {"api_key"=>"ff4a6fa1c975693bedc2122e6943946b", "country_id"=>"9", "birthday_year"=>"1989", "birthday_month"=>"2", "birthday_day"=>"16", "gender"=>"42198", "hispanic"=>"42200", "zip"=>"53593", "state"=>"45700", "ethnicity"=>"42215", "standard_relationship"=>"42232", "standard_education"=>"42243", "standard_hhi_us"=>"43511"}
User Load (18.7ms) SELECT "users".* FROM "users" WHERE "users"."api_key" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["api_key", "ff4a6fa1c975693bedc2122e6943946b"]]
rake routes output from development:
api_v1_register_panelist GET /api/v1/register_panelist(.:format) api/v1/appusers#register_panelist {:format=>"json"}
api_v1_get_surveys GET /api/v1/get_surveys(.:format) api/v1/appusers#get_surveys {:format=>"json"}
and production server:
api_v1_register_panelist GET /api/v1/register_panelist(.:format) api/v1/appusers#register_panelist {:format=>"json"}
api_v1_get_surveys GET /api/v1/get_surveys(.:format) api/v1/appusers#get_surveys {:format=>"json"}

I would guess that there is something in the web server configuration that is usurping the request before it gets passed to Rails (mod rewrite or the like), or that the mount point setting of the Rails execution environment within your web server (e.g. Unicorn in Nginx, Passenger in Apache) , or maybe a piece of middleware that is handled differently by WEBrick and your live server.
Whatever the case, I would start by checking the routes on the server:
rake routes
and make sure they are what you expect. Next, inspect the rails logs and verify that the request is actually being handled by Rails and not being intercepted by the web server or some middleware. If you crank log level up to debug it should give details about exactly how it is handling it. This should at least give you the next step for where to look.

I'm not positive why this works, but it seems I had to update my routes.rb file to call out that line twice - once within the constraints to find the routes in my development environment, and once without to be found in production:
constraints(ApiConstraint) do
namespace :api, defaults: {format: 'json'} do
namespace :v1, defaults: {format: 'json'} do
#find in development
match "register_panelist", to: "appusers#register_panelist", via: 'get'
match "get_surveys", to: "appusers#get_surveys", via: 'get'
end
end
match "api/v1/*path", to: "api/v1/misc#api_not_found_404", via: :all, format: 'json'
end
#find in production
match "api/v1/register_panelist", to: "api/v1/appusers#register_panelist", via: 'get'
match "api/v1/get_surveys", to: "api/v1/appusers#get_surveys", via: 'get'

Related

Rails - Two or more dots in the url

I have a rails app where a route is defined as
get "/:username", to: "profiles#show", as: :show_profile, constraints: UsernameConstraints.new
And UsernameConstraints is defined as
class UsernameConstraints
#Some code
def matches?(request)
request.path.match?(/.*/)
end
#Some code
end
So that usernames can be something like myapp.com/user.name
How do I change this code to accept usernames with two or more dots (myapp.com/something.like.this)?
At the moment I can't get rid of the No route matches error.
I tried adding format: true to the route and re-building the username in the controller using the parameters:
myapp.com/something.like.this
{"username"=>"something", "format"=>"like.this"}
params[:username] = "#{params[:username]}.#{params[:format]}"
But it’s not a clean solution and also the route does not match any more simple usernames like /username
get "/:username", to: "profiles#show",
as: :show_profile,
username: /[^\/]+/
Started GET "/foo.bar.baz" for ::1 at 2021-01-25 01:19:18 +0100
Processing by ProfilesController#show as HTML
Parameters: {"username"=>"foo.bar.baz"}
Hello World!
Completed 204 No Content in 0ms (ActiveRecord: 0.0ms | Allocations: 45)

Rails route not found even when its present

My following API route is not found when hit with CURL or POSTMAN client
Started POST "/api/users/register_handheld" for 181.74.100.34 at 2017-05-11 11:27:42 +0000
DEBUG: Chewy strategies stack: [2] <- atomic # /home/deploy/boold/shared/bundle/ruby/2.2.0/gems/chewy-0.8.4/lib/chewy/railtie.rb:17
ActionController::RoutingError (No route matches [POST] "/api/users/register_handheld"):
But when I run rake routes, this URL is present.
api_users_register_handheld POST /api/users/register_handheld(.:format)
crowd/api/v1/handheld_users#create {:format=>"json"}
This URL actually works if I hit in rails console:
app.post "/api/users/register_handheld"
Started POST "/api/users/register_handheld" for 127.0.0.1 at 2017-05-11 11:21:03 +0000
DEBUG: Chewy strategies stack: [3] <- atomic # /home/deploy/boold/shared/bundle/ruby/2.2.0/gems/chewy-0.8.4/lib/chewy/railtie.rb:17
Processing by Crowd::Api::V1::HandheldUsersController#create as JSON
Routes file is
namespace :api, defaults: {format: 'json'} do
scope module: :v1 do
resources :dashboard, controller: "account/dashboard"
post '/account/settings/create_notification' => 'account/settings#create_notification'
post '/users/register' => 'users#create'
post 'users/register_handheld' => 'handheld_users#create'
put 'users/update_profile' => 'handheld_users#update_profile'
post 'users/login' => 'handheld_users#login'
NOTE: this repo is Rails Engine
try below code:
/api/users/register_handheld.json
as you specified format type json
Maybe this helps someone.
Using POSTMAN i accidentally uncheck Host Key in headers (POSTMAN autogenerate it each time you send any request). It was a nightmare to find what's the problem until i compared my request with a similiar one and found the missing key.

Turning resources into custom routes (Ruby/rails)

Started building an application here. client and server style architecture sending active resources across the wire and storing as activeRecord server side. Managed to get it up and running with a nice example in an O Reilly book except its using scaffold.
Rails routing - custom routes for Resources
is using map.resources from rails 2-
I'm using rails 3 so Its not really applicable and while I did post a question about routes from 2 to 3, I still cant convert this.
So here whats im looking at:
Rake routes with
resources :user_requests
gives:
user_requests GET /user_requests(.:format) {:controller=>"user_requests", :action=>"index"}
POST /user_requests(.:format) {:controller=>"user_requests", :action=>"create"}
new_user_request GET /user_requests/new(.:format) {:controller=>"user_requests", :action=>"new"}
edit_user_request GET /user_requests/:id/edit(.:format) {:controller=>"user_requests", :action=>"edit"}
user_request GET /user_requests/:id(.:format) {:controller=>"user_requests", :action=>"show"}
PUT /user_requests/:id(.:format) {:controller=>"user_requests", :action=>"update"}
DELETE /user_requests/:id(.:format) {:controller=>"user_requests", :action=>"destroy"}
I'd like to remove this and the resources and have my own routes pointing to my own defs.
Heres a quick attempt
match '/user_requests(.:format)' => 'user_requests#create , :via =>:post'
match '/user_requests/:id(.:format)' =>"user_requests#show"
returns almost the exact same as above
/user_requests(.:format) {:controller=>"user_requests", :action=>"create"}
/user_requests/:id(.:format) {:controller=>"user_requests", :action=>"show"}
With the exception of the REST nouns at the start and the links. Its the same yet my own routes dont work.
What do I need to add to my routes to make them do the same thing as resources?
I'm not keeping scaffold as I've been told its never used in the real world. And I will be changing the names of my defs, but one step at a time.
Error that server shows:
Started POST "/user_requests.xml" for 127.0.0.1 at Tue Jul 12 17:13:32 +0100 2011
Processing by UserRequestsController#create as XML
Parameters: {"method"=>"POST", "user_request"=>{"depth"=>3000000, "url"=>"www.stackoverflow.com"}}
SQL (0.1ms) SELECT 1 FROM "user_requests" WHERE ("user_requests"."url" = 'www.stackoverflow.com') LIMIT 1
AREL (0.3ms) INSERT INTO "user_requests" ("updated_at", "depth", "url", "created_at") VALUES ('2011-07-12 16:13:32.765392', 3000000, 'www.stackoverflow.com', '2011-07-12 16:13:32.765392')
Completed 404 Not Found in 17ms
ActionController::RoutingError (No route matches {:controller=>"user_requests", :id=>#<UserRequest id: 6, url: "www.stackoverflow.com", depth: 3000000, created_at: "2011-07-12 16:13:32", updated_at: "2011-07-12 16:13:32">, :action=>"show"}):
app/controllers/user_requests_controller.rb:19:in `create'
app/controllers/user_requests_controller.rb:16:in `create'
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.9/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.8ms)
It would guess that line 19 of your UserRequestsController has something like
redirect_to #user_request
which tries to guess a URL for showing a UserRequest object. Rails no longer knows how to do this; you've lost the helper methods generated by resources (such as user_request_path, new_user_request_path, etc).
You can tell Rails to generate the "show" helper method by adding a :as option to your show route, without the _path postfix:
match '/user_requests/:id(.:format)' => "user_requests#show", :as => 'user_request'
You'll now have access to a user_request_path and user_request_url, which Rails can use to find the URL to "show" a UserRequest.

Rails routing error on valid route

I have the following in my routes file:
resources :timelogs do
member do
post :stop
end
collection do
get :start
end
end
which produces the following on 'rake routes' :
rake routes | grep stop
stop_timelog POST /timelogs/:id/stop(.:format) {:action=>"stop", :controller=>"timelogs"}
However, when posting a request to that URL I'm seeing:
Started POST "/timelogs/325/stop" for 188.220.17.64 at Wed Nov 24 02:22:22 -0800 2010
ActionController::RoutingError (No route matches "/timelogs/325/stop"):
All of this looks like it should be working, however, it's not. What could be the problem here?
I see no problem with the routes you've pasted and have verified that they work for me in an scratch app.
Started POST "/timelogs/123/stop" for 127.0.0.1 at 2010-11-24 11:49:25 +0000
Processing by TimelogsController#stop as */*
Parameters: {"a"=>"b", "id"=>"123"}
Rendered text template (0.0ms)
Completed 200 OK in 60ms (Views: 59.9ms | ActiveRecord: 0.0ms)
Perhaps something else in your routes.rb is in conflict here?
Actually when you are trying to send your form with exists resource (ticket) rails by default will send PUT request, so you should set :method => :post clear or change route from
post :resolve, :on => :member
to
put :resolve, :on => :member

Rails route hanging

I'm trying to get someone else's app up and running on my development laptop but I ran into a routing issue and I'm not sure how to debug it. For a particular controller/action, it just hangs and doesn't time out and there is no error message in the development log. Does anyone know how I can debug this? Thanks.
Edited per comments.
config.rb
ActionController::Routing::Routes.draw do |map|
map.signup "/signup", :controller => "business_accounts", :action => "new"
map.resources :beta_signups, :controller => 'public/beta_signups'
map.root :controller => "public/pages", :action => "index"
end
For brevity, I commented out the rest of the routes and left in a couple of routes that work. The one that failed is the signup route, it simply hangs and never times out.
Here's are the relevant output from the development.log showing a route that works (root) and one that doesn't (signup)
Parameters: {"action"=>"index", "controller"=>"public/pages"}
Rendering template within layouts/public
Rendering public/pages/index
Completed in 672ms (View: 656, DB: 15) | 200 OK [http://localhost/]
SQL (0.0ms) SET client_min_messages TO 'panic'
SQL (0.0ms) SET client_min_messages TO 'notice'
Processing BusinessAccountsController#new (for 127.0.0.1 at 2010-04-22 10:01:30)
[GET]
Parameters: {"action"=>"new", "controller"=>"business_accounts"}
Not sure if this makes any difference but it is running on thin and bundler.
Stripped the controller down to the bare minimum and still getting the same error
class BusinessAccountsController < SSLController
def new
logger.debug "here"
end
end
And I just noticed SSLController, hmm, I need to look into that.
Son of a !#%$##%$%!$#!%, SSLController was my problem, there is no SSL setup on my laptop and the net effect is the app hanging with no error message. Changing it to ApplicationController works. Thanks #Taryn, it was your request that made me looked closer at the code, funny how I've looked at it for days and never saw it till now.

Resources