Rails routing error on valid route - ruby-on-rails

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

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.

rails routing duplicates path

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

rails routing show action nested resources

Yeah the problem is, that I created a nested resource like this:
resources :albums do
resources :elements
end
and the rake routes command displays:
album_element GET /albums/:album_id/elements/:id(.:format) elements#show
So when I am at
.../albums/1
I can head for
.../albums/1/elements
This starts the index action of the elements controller, just fine.
But if I edit the index.html.erb to
<%= link_to 'Show', album_element_path %>
I got an error like this:
Started GET "/albums/1/elements" for 176.221.47.67 at Tue Oct 09 14:25:39 +0200 2012
Processing by ElementsController#index as HTML
Parameters: {"album_id"=>"1"}
Rendered elements/index.html.erb within layouts/application (9.2ms)
Completed 500 Internal Server Error in 123ms
ActionController::RoutingError (No route matches {:controller=>"elements", :action=>"show"}):
app/views/elements/index.html.erb:29:in `_app_views_elements_index_html_erb___13604879__168097178'
app/views/elements/index.html.erb:18:in `each'
app/views/elements/index.html.erb:18:in `_app_views_elements_index_html_erb___13604879__168097178'
app/controllers/elements_controller.rb:7:in `index'
So it says that no route matches ... but I actually have that in my rake routes displayed ?
What am I doing wrong ?
You need to provide the two neccesary arguments for album_element_path:
<%= link_to 'Show', album_element_path(#album, #element) %>

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.

Resources