rails routing duplicates path - ruby-on-rails

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

Related

NoMethodError users_url with devise (ajax)

I use devise 2.2.2 with rails 3.2.11
I use devise with ajax requests
I changed the following configuration in initializers/devise.rb
config.navigational_formats = [:json, :html]
config.http_authenticatable_on_xhr = false
when I submit an empty sign in request, I expect to get a json response with errors hash, but i get a 500 instead (see below for the trace) (it works fine with sign up request)
here are my routes (nothing special)
devise_for :users
the trace:
Started POST "/users/sign_in.json" for 127.0.0.1 at 2013-01-27 13:33:45 +0100
Processing by Devise::SessionsController#create as JSON
Parameters: {"user"=>{"email"=>"", "password"=>"[FILTERED]"}}
Completed 401 Unauthorized in 1ms
Processing by Devise::SessionsController#new as JSON
Parameters: {"user"=>{"email"=>"", "password"=>"[FILTERED]"}}
Completed 500 Internal Server Error in 40ms
NoMethodError (undefined method `users_url' for #<Devise::SessionsController:0x007fe88ddd9550>):
You are probably overriding after_sign_in_path_for and have a code path in there that returns nil.
This causes devise to fall back to its default behaviour and call users_url to get the path to redirect to.
Why do I think this? Because you are having the same error I had (and lost some hair over) and also this bug report contains the github usernames of many other people who have been humbled by this particular issue.

rails 3.1.0 devise with cancan getting unauthorized with invalid username and/or password

I have a fairly simple app using devise and cancan for authentication and authorization. Everything works great except when users try signing in with invalid usernames and/or passwords. When this happens, we get an error loading page with the following exception in the logs:
Started POST "/users/sign_in" for 127.0.0.1 at 2012-02-09 22:23:22 -0600
Processing by Devise::SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"blahblahblahblah", "user"=>{"login"=>"asdasd", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign in"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE (lower(username) = 'asdasd' OR lower(email) = 'asdasd') LIMIT 1
Completed 401 Unauthorized in 74ms
I'm not sure what I need to set to allow the authorization and/or how to get more detailed logs to see exactly what is not authorized? If I enter valid credentials I can access the application and all other pieces of the app work as expected.
I know this question has been posted a couple months ago but I hot the same issue, and after fighting it for a few hours, overwriting Devise SessionController, Devise Custom Failure, debugging through, etc.., I finally found out what was going on and I decided to share the solution so you guys won't have to go through that.
The error 'Completed 401 Unauthorized in XXms' happens within the create method of SessionController at the line:
resource = build_resource(...)
And the resource cannot be built since the resource is not passed to the server. Now the problem resides in WHY the resource isn't passed to the server? I'm using JQUERY mobile which post the sign_in as an AJAX call, which JQUERY cannot upload data like that through AJAX.
You need to add to your signin form the data-ajax=false:
in Devise/sessions/new.html.erb modify the form to look like this:
form_for(resource, :as => resource_name, :url => user_session_url, html: {data: {ajax: false}}) do |f|
Hope that helps someone down the road.

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