I am using this rule:
match '*path', via: :all, to: 'application#index'
to redirect all unknown routes to application#index.
However, it does not catch POST /, it returns the following error:
No route matches [POST] "/"
Is there a way to include this path without adding more rules?
Related
I got this route
scope '/:slug' do
get '/', to: 'sites#home', as: :site
get '/:resource/:id', to: 'sites#resource', as: :resource
get '/:id', to: 'sites#page', as: :page
end
And it is conflicting with my websocket default route. If I remove the code above the websocket starts working fine.
I want to make sure those 3 statements will not match if the URL starts with ws:// or ends with /websocket
How can I do it?
As far as I read, I understood that Rails 4 doesn't support match keyword.
But I want to make a default generic route something like following
match ':controller(/:action(/:id))(.:format)', :via => :get
When I use above code in routes.rb, it throws me following error:
Missing :action key on routes definition, please check your routes
Can someone help me how to implement this in Rails 4.
Yes it is supported, from the Rails Routing from the Outside In guide
3.7 HTTP Verb Constraints
In general, you should use the get, post, put, patch and delete methods to constrain a route to a particular verb. You can use the match method with the :via option to match multiple verbs at once:
match 'photos', to: 'photos#show', via: [:get, :post]
You can match all verbs to a particular route using via: :all:
match 'photos', to: 'photos#show', via: :all
and from the documentation:
http://api.rubyonrails.org/classes/ActionDispatch/Routing/Mapper/Base.html#method-i-match
# sets :controller, :action and :id in params
match ':controller/:action/:id', via: [:get, :post]
If you add this line to your routes:
match ':controller/:action/(:id)', via: :get
it will produce the following route:
Prefix Verb URI Pattern Controller#Action
GET /:controller/:action(/:id)(.:format) :controller#:action
I am getting this error and can not figure out why. All my other routes work in local and remote when I open in browser but users. I am totally new to ruby and Xcode
thanks for any help
Looks to me like you need to also specify the :index action when defining the users routes:
ParkApp::Application.routes.draw do
namespace :api do
namespace :v1 do
resources :users, only: [:index, :create, :update] do
collection do
match '/me', to: 'users#show', via: :get
end
end
## Needed to allow cross origin request from webapp
match '/users/:id', to: 'users#update', via: :post
resources :sweetches, only: [:create, :update, :index]
match '/sweetches/:id', to: 'sweetches#update', via: :post
# Get the messages to display on the views
match '/message_views', to: 'message_views#index', as: :message_views, via: :get
resources :posts
end
end
match '/admin', to: 'admin#index', via: :get
match '/admin', to: 'admin#create', via: :post
match '/admin/:id', to: 'admin#destroy', as: :delete_fake, via: :delete
end
This should make the route you are trying to follow valid. If you take a look at the last snippet in your question you will see that the api/v1/users GET route isn't defined, my change to your routes will fix that.
You are getting the error because, as the error says, your application does not have a route that matches "/users".
See api_v1_users_path POST /api/v1/users(.:format) api/v1/users#create in your post. This means that the POST request to path /api/v1/users invokes the create method of your API::V1::UsersController. So send request to /api/v1/users, but not to '/users`.
I've never used Objective-C, but I guess you need to replace NSString stringWithFormat:#"/users" with NSString stringWithFormat:#"/api/v1/users".
Note that you need to send a POST request. Opening the above path in browser sends a GET request to your application, resulting in 404 not found error.
Im using RoR to write my website and i've run into this issue with routes.
My routes file looks as such
root to: "static#index"
match '/index', to: 'static#index', via: [:get, :post]
match '/download', to: 'static#download', via: [:get, :post]
match '/terms', to: 'static#terms', via: 'get'
match '/privacy', to: 'static#privacy', via: 'get'
match '/jobs', to: 'static#jobs', via: 'get'
match '/android', to: 'static#android', via: [:get, :post]
match '/apply', to: 'static#apply', via: 'get'
match '/faq', to: 'static#faq', via: 'get'
When I preform a GET request to https://www.website.com/ the page gets displayed.
However when I preform a POST request to the same address it returns a 404 page not found.
I do not have a index.html in my public directory.
So why is this POST request returning a 404 when it is matched to both GET and POST and the GET works correctly?
EDIT
i've added this line
match '/', to: 'static#index', via: [:get, :post]
just below my root to declaration, however now my page shows a 422 error instead of a 404
You can try this:
post '/' => "static#index", as: "root"
Rails routes: resourcing from the root path "/"
Your root URL is the one that handles https://www.website.com/ from your example. When you specify
root to: "static#index"
You have declared a GET route for /, which maps to StaticController#index. The POST request that you are making to https://www.website.com/ is not handled by the root route, thus properly returns a 404.
I'm guessing that you want the POST request to go to either /index, /download or /android. You need to specify the endpoint fully in the URL in order for the request to be routed properly.
My preference and I think the cleanest implementation would be:
resources :static, :only => [:index, :create], :path => "/"
I have a "list" model and "ListsController" controller for it. By default, the route for lists was /lists/1, /lists/1/edit/, etc. I changed my routes.rb file to make it so the show path was "/:id", the new path was "/new".
Here's my routes file:
ToDo::Application.routes.draw do
root to: 'pages#home'
match '/about', to: 'pages#about'
match '/contact', to: 'pages#contact'
match '/help', to: 'pages#help'
resources :lists
match '/new', to: 'lists#new'
match '/:id', to: 'lists#show'
match '/:id/new', to: 'lists#new_item'
end
I can access a list by doing "localhost:3000/1" perfectly fine. But now I'm trying to use link_to, and when I do "link_to "List", list", it generates a url to the original route, which is "localhost:3000/lists/1".
Does anyone know how to fix this? Is there anything I should be doing better with my routes?
Thanks!
Instead of using match you could simply provide alternative path for resources:
resources :lists, path: ''
You will need to specify as: 'name' option to create a named route for your match rules, and to overwrite the named route provided by resource :lists.
resource :lists
match '/new', to: 'lists#new', as: 'new_list'
match '/:id', to: 'lists#show', as: 'list'