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ðnicity=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ðnicity=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'
I have a simple enough problem of taking a rails 2 route and making it working with rails 4. The rails 2 version is this:
map.token '/sessions/token', :controller => 'sessions', :action => 'token'
I've changed it to:
get '/sessions/token', to: 'sessions#token', as: '/token'
for rails 4. The problem is when I go to /sessions/token it immediately redirects to /sessions/token/sessions/token and gives a 404
I added byebug to the token method but the request never makes it there before redirecting. I added it early in the application controller but I reach a point where the next function just stops working.
The log produces this when requesting https://delete-me.domain.org/sessions/token:
[delete-me] Started GET "/sessions/token" for 127.0.0.1 at 2015-10-02 05:44:27 -0500
[delete-me] Processing by SessionsController#token as HTML
[delete-me] Redirected to http://delete-me.domain.org/sessions/token/sessions/token
[delete-me] Filter chain halted as :ensure_proper_protocol rendered or redirected
[delete-me] Completed 302 Found in 114ms (ActiveRecord: 5.8ms)
I think it gives the :ensure_proper_protocol error because it redirects to http from https. I don't know why it does that. Maybe someone has had a similar problem and they can enlighten me.
#config/routes.rb
resources :sessions do
get :token, on: :collection #-> /sessions/token (sessions#token controller action)
end
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.
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
I am trying to set up a sinatra app inside my Rails 3 (v3.0.1) application, but having no success. Sinatra gem (v1.1.0) is setup using bundle install.
Here's what i have.
customer_app.rb class in lib directory -
class CustomerApp < Sinatra::Base
get "/test" do
"Hello World"
end
end
my routes.rb file contains -
CustomerService::Application.routes.draw do
root :to => CustomerApp
end
The URL i am trying is - http://localhost:3000/test
I get this error (on the browser) - Routing Error. No route matches "/test"
and this error in the log - ActionController::RoutingError (No route matches "/test"):
Is there anything I am missing??
Also I just noticed, even a simple rack route is not working -
root :to => proc { |env| [200, {}, ["Welcome!"]]}
The root keyword by default maps only the path /.
So you are trying to say, forward any request for / to CustomerApp which can handle requests for /test.
You should change the match filter.
CustomerService::Application.routes.draw do
match "/test" :to => CustomerApp
end