Rails route appears in routes but throws a 404 - ruby-on-rails

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'

Related

Rails routes appear but throws a 404 error

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.

Use a namespace only as param

At the moment my routes are looking like this:
search_index GET /search(.:format) search#index
But I want to get routes like these:
search_index GET /topic1/search(.:format) search#index
search_index GET /topic2/search(.:format) search#index
As you can see, I don't want to introduce namespaces. The "topic" part of the url shall only be transported as a param.
in your routes.rb append:
match '/:topic_name/search' => 'search#show', via: :get
this will add topic_name to your params, accessible in controller - params[:topic_name]

Rails routing /forum/[category_id]/[topic_id]

I would like to create nested routes for my forum, where a topic belongs to a category.
I'm not liking the default nested routes
resources :forum_category do
resources :forum_topic
end
which would give me something like /forum_category/[forum_category_id]/forum_topic/[forum_topic_id]
What I want:
I'm looking to create the following rules (leaving out POST, PATCH, PUT routes)
/forum => forum_topic#index
/forum/new => forum_topic#new
/forum/[forum_category_id] => forum_topic#index
/forum/[forum_category_id]/[forum_topic_id] => forum_topic#show
So /forum is my index, forum/open-mic is my index limited to category with seo slug open mic and finally /forum/open-mic/lets-talk-about-fun would be my forum topic lets-talk-about-fun which is categorized under open-mic.
Is there any solution for this build into Rails?
If you're just looking for GET requests you can do this:
get '/forum', to: "forum_topic#index", as: :forum_topics
get '/forum/new', to: "forum_topics#new", as: :new_forum_topic
get '/forum/:forum_category_id', to: "forum_topics#show", as: :forum_topic_category
get '/forum/:forum_category_id/:forum_topic_id', to: "forum_topic#show_topic", as: :show_forum_topic_category
You could map the last two to the same controller action and redirect based on params, but I'd recommend setting up two separate actions for readability.
you can do like this for customize routes:
match "<your-url>", to:"<controller>#<action>", as: "<path-name>", via: :<your method-like GET,POST,DELETE>
for example to GET:
match "forum_category/:forum_category_id/forum_topic/:forum_topic_id", to: "forum_topic#show", as: "show_topic", via: :get
and for POST:
match "forum_category/:forum_category_id/forum_topic", to: "forum_topic#update", as: "update_topic", via: :post
in your controller you have this params:
param[:forum_category_id] and param[:forum_topic_id]

Rails route shows in rake, responds in console,but browser shows undefined method?

I cannot figure out why this route is not working. I have defined it, and it seems that it works everywhere, except for in the browser. I've attached screenshots with the relevant information.
The route for reports_path is showing undefined in the browser, but everywhere else it seems that it's working. What could be causing this?
rake routes shows reports_path, don't use path when naming you routes, you have one more named with path reports_selling_agent_performance_path, remove the path.
in this case:
match 'reports', to: 'reports#index', as: :reports
or
match 'reports', to: 'reports#index'
will work too, you use as: key only if you want to rename the path name that you'll use in code, for example:
match 'my_long_very_long_reports', to: 'reports#index', as: :reports
and you'll use reports_path in code instead of my_long_very_long_reports_path. Or in cases where you use keys like :id or others:
match 'my_reports/:id', to: 'reports#index', as: :reports
where :id will be id of the user for example that you'll send as params[:id]
In your routes file, try changing as: :reports_path to as: :reports otherwise I think you'll need to use reports_path_path in your view.

Routing error for GET] "/firstapp"

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.

Resources