I'm trying to thrown a 404 error without redirect for unknown routes. It works correctly but when I go to the show routes, that it is in the rails routes, it throws a 404 too.
Relevant section from routes.rb
get '(/:department)', to: 'products#index'
get 'saldao', to: 'products#on_sale', as: :on_sale
get '*missing', to: 'error#error_404', as: :error_404
get '(:department)(/:brand)(/:os)', to: 'products#index', as: :products
get '(:department)(/:brand)(/:os)/:slug', to: 'products#show', as: :product
It`s weird because Product#show appears in the routes when i check:
products GET (/:scope)(/:department)(/:brand)(/:os)(.:format) frontend/products#index {:scope=>/pre|loyalty|corporativo|diaconsumidor|troca4g|troca3g4g|outlet|j4|z3|j6/, :department=>/celulares|acessorios|pos|controle|wttx/, :brand=>/alcatel|apple|asus|lenovo|lg|motorola|nokia|philips|positivo|samsung|sony|tim|wnc/, :os=>/android|ios|windows-phone/}
I've tried a lot of things and haven`t been succesfull.
The error route also stops work with I move it to the end of file.
Related
I'm a newbie to coding and have been using SO to look for this answer. People have the same error but I can't figure out how it relates to my problem.
I'm working on a Pinterest clone but I keep getting this error in the command line when I add pictures. ActionController::RoutingError (No route matches [GET] "/pin_images/original/missing.png"):
How would I add it to the routes? I thought it would already be allowed
Rails.application.routes.draw do
resources :pins
devise_for :users
root 'home#index'
get 'new/pins'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
I'm trying to add a simple route to an existing controller/action but strangely I'm getting a 404 error even though the route appears to exist.
Here's the relevant section of my routes.rb:
# Wines
scope 'wine' do
get '/', to: 'wines#index', as: 'wine_index'
get '/:collection', to: 'wines#collection_detail', as: 'collection_detail'
get '/:collection/:slug', to: 'wines#wine_detail', as: 'wine_detail'
get '/:style', to: 'wines#style_detail', as: 'style_detail'
end
It seems correct because here's what I see when I check:
$ rake routes
=>
Prefix Verb URI Pattern Controller#Action
wine_index GET /wine(.:format) wines#index
collection_detail GET /wine/:collection(.:format) wines#collection_detail
wine_detail GET /wine/:collection/:slug(.:format) wines#wine_detail
style_detail GET /wine/:style(.:format) wines#style_detail
GET|POST /*path(.:format) pages#error404
I also see an expected response in the console:
2.3.1 :003 > app.style_detail_path('semi-dry')
=> "/wine/semi-dry"
Yet, when I try to visit /wine/semi-sweet/ (Semi-sweet is a style "slug" which I use to search in the action) I get a 404 error.
What could I me missing? I've searched dozens of similar questions on S.O. and none of the solutions apply to my situation.
It seems you need to specify constraints. When you say 'wines/semi-sweet', how would the router decide whether it's a style_detail path or a colletion_detail path? They both have the same mask '/wines/:something'
It should be something like:
scope 'wine' do
get '/', to: 'wines#index', as: 'wine_index'
get '/:style', to: 'wines#style_detail', as: 'style_detail', constraints: proc { |r| Style.include?(r.params[:style]) }
get '/:collection', to: 'wines#collection_detail', as: 'collection_detail'
get '/:collection/:slug', to: 'wines#wine_detail', as: 'wine_detail'
end
This way the router will match predefined words (could be an array too) with wine styles, all the other strings will be considered as wine collections.
But it would be best to change the mask for these two paths, just to be safe, for example:
get '/:style', to: 'wines#style_detail', as: 'style_detail'
get '/c/:collection', to: 'wines#collection_detail', as: 'collection_detail'
After running rails generate controller home index in my Rails application, I see this line in routes.rb
get "home/index"
What does this line do? When I removed it, I didn't observe any difference it makes.
see the Rails Routing page for more info but...
It adds, to the routing table, an entry to direct a GET request of the form
http://localhost:3000/home/index
To the HomeController#index action, which will render a response and display the results to the user.
It is a shorthand notation for
match 'home/index' => 'home#index', :via => :get
To see what other routes your application has available, run the following from a terminal while inside your projects directory
rake routes
I was trying to write my first program after installation, but I got an error like below:
Routing Error
No route matches [GET] "/firstapp"
I've tried to change my config/routes.rb file but nothing changed. This is my config/routes.rb
Firstapp::Application.routes.draw do
resources :apptables
# The priority is based upon order of creation:
# first created -> highest priority.
# continues with default `config/routes.rb` explanations...
end
How can I configure the config/routes.rb to make it run properly?
Just saying resources :apptables sets up the standard seven routes:
GET /apptables
GET /apptables/new
POST /apptables
GET /apptables/:id
GET /apptables/:id/edit
PUT /apptables/:id
DELETE /apptables/:id
There is no /firstapp in that list so that route won't work. If you want a GET on /firstapp to work then you can set up that route manually:
match '/firstapp' => 'firstapp#some_method', :via => :get
That would route GET /firstapp to FirstappController#some_method.
I have a Rails app that has a controller called domain which has a nested controller called subdomain and stats. I have defined them in routes.rb:
resources :domains do
resources :subdomains, :stats
end
I have changed the to_param of the domain and subdomain models to use the name of the domain, e.g.: the routing I get is http://site/domains/foo/subdomains/bar.
I would like to tidy it up to so that instead of using http://site/domains/foo/subdomains/bar I could access it with just http://site/foo/subdomains/bar. I have tried the following in routes.rb:
match "/:id/" => "domains#show", :as => :domain
Which works fine, but it only gives me the ability to use the path http://site/foo but for example http://site/foo/subdomains/bar doesn't. I could create match lines for every respective model and nested model but that does nothing to other helpers besides domain_url - i.e. edit_domain_url points to /domains/foo/edit/ instead of /foo/edit.
Is there a way to change the routing so that the resources generates helpers that point to the root url without the 'domains' part?
The single match in your routes creates only one route. Resource helpers create many routes at once. Luckily there are a lot of options for customisation. If you want to omit /domains/ from your paths, it's as simple as:
resources :domains, :path => "/" do
resources :subdomains, :stats
end
With the above in config/routes.rb, running rake routes says the following:
domain_subdomains GET /:domain_id/subdomains(.:format)
domain_subdomains POST /:domain_id/subdomains(.:format)
new_domain_subdomain GET /:domain_id/subdomains/new(.:format)
edit_domain_subdomain GET /:domain_id/subdomains/:id/edit(.:format)
domain_subdomain GET /:domain_id/subdomains/:id(.:format)
domain_subdomain PUT /:domain_id/subdomains/:id(.:format)
domain_subdomain DELETE /:domain_id/subdomains/:id(.:format)
domain_stats GET /:domain_id/stats(.:format)
domain_stats POST /:domain_id/stats(.:format)
new_domain_stat GET /:domain_id/stats/new(.:format)
edit_domain_stat GET /:domain_id/stats/:id/edit(.:format)
domain_stat GET /:domain_id/stats/:id(.:format)
domain_stat PUT /:domain_id/stats/:id(.:format)
domain_stat DELETE /:domain_id/stats/:id(.:format)
domains GET /(.:format)
domains POST /(.:format)
new_domain GET /new(.:format)
edit_domain GET /:id/edit(.:format)
domain GET /:id(.:format)
domain PUT /:id(.:format)
domain DELETE /:id(.:format)
Looks like all the routes you need!