Why is my route ignored for one below it? - ruby-on-rails

I have code that specifies the following routes in my routes.rb:
concern :searchable do
get "search(/:query)", controller: "search", action: :show, as: "search"
end
scope module: 'custom_domains', as: "custom_domain" do
root to: "pages#show", id: "index"
concerns :searchable
get '/:id' => "pages#show", as: "page"
match "*path" => "redirects#show", via: [ :get ], as: "redirect"
end
/
/search(/:query)(.:format)
/:id(.:format)
/*path(.:format)
When I try to visit /search, the route is skipped and catch all *path matches the route. I'm stumped. Routes are defined top to bottom and match in that order, so I would think that /search would be correct match. Do you know why this isn't the case?

Related

Rails: Sub-routing. No route matches

I have custom CRUD routes for example - for profiles
get '/profiles', to: 'profiles#index'
get '/profiles/new', to: 'profiles#new', :as => 'new_profile'
post '/profiles', to: 'profiles#create'
get '/profiles/edit/:id', to: 'profiles#edit', :as => 'profile'
patch '/profiles/edit/:id', to: 'profiles#update'
get '/profiles/get_profiles', to: 'profiles#get_profiles'
It works okay. But I do the same routing for profile skills, wich is under relation of profile. Routes of ProfileSkills looks like this
get '/profiles/:profile_id/profile_skills', to: 'profile_skills#index'
get '/profiles/:profile_id/profile_skills/new', to: 'profile_skills#new', :as => 'new_profile_skill'
post '/profiles/:profile_id/profile_skills', to: 'profile_skills#create'
get '/profiles/:profile_id/profile_skills//edit/:id', to: 'profile_skills#edit', :as => 'profile_skills'
patch '/profiles/:profile_id/profile_skills/edit/:id', to: 'profiles#update'
When I under the route for creating the new item
http://localhost:3000/profiles/1/profile_skills/new
It throw an exception
No route matches {:action=>"edit", :controller=>"profile_skills", :profile_id=>"1"}, missing required keys: [:id]
On form for line
<%= form_for #profile_skill do |form| %>
Why he don't understand that I'm under the 'new' route and it looking for 'edit', when i'm under the 'new'?
This problem is only when i'm on sub-routes. In 'Porfile' routes for example, if works fine.
In your routes use this
resources :profiles do
resources :profile_skills
end
this will provide you the routes like this
profiles/:profile_id/profile_skill points to index action of profile_skill
profiles/:profile_id/profile_skill/new points to new action of profile_skill
profiles/:profile_id/profile_skill/:profile_skill_id points to show action of profile_skill
profiles/:profile_id/profile_skill/:profile_skill_id/edit points to edit action of profile_skill
and so on.
for more help visit Rails Routing

Why Rails dont recognize route

I have in routes.rb
get '/cities/:city/:section' => 'cities#show', as: 'city_section_slug'
get '/cities/:city/:section/:subsection' => 'cities#show', as: 'city_subsection_slug', :constraints => { :subsection => /[^\/]*/ }
And rake show these routes
city_section_slug GET (/:locale)/cities/:city_id/:section(.:format) cities#show {:locale=>/ru|en/}
city_subsection_slug GET (/:locale)/cities/:city_id/:section/:subsection(.:format) cities#show {:subsection=>/[^\/]*/, :locale=>/ru|en/}
But when I try create a link:
= link_to city_subsection_slug_path(#city,section.alias, subsection.alias)
Iv got such error:
ActionController::RoutingError (No route matches {:controller=>"cities", :action=>"show", :locale=>:ru, :city=>#<City id: 42, name: "City">, :section=>"events", :subsection=>"sobytiya/ya-ochevidets"}):
Any ideas where I am wrong?
The reason is your constraint on subsection param. You specified that it can be anything as long as it does not contain / character. Your subsection does conatin it hence the error. If you want to allow / character, then do:
get '/cities/:city/:section/:subsection' => 'cities#show',
as: 'city_subsection_slug',
constraints: { :subsection => /^[a-z0-9_\/]+$/ }
Eventually, you can use glob param:
get '/cities/:city/:section/*subsection' => 'cities#show',
as: 'city_subsection_slug',
#config/routes.rb
resources :cities, only: [] do
get ":section", action: :show, as: :city_section_slug #-> url.com/cities/:city_id/:section
get ":section/*subsection", action: :show, as: :city_subsection_slug #-> url.com/cities/:city_id/:section/:subsection
end
The corresponding links:
<%= link_to "x", city_section_slug_path(#city, section.alias) %>
<%= link_to "x", city_subsection_slug_path(#city, section.alias, subsection.alias) %>
Wildcard
If you're expecting to send sobytiya/ya-ochevidets to your subsection path, you'll be best using a wildcard route

Set non-conflicting toplevel path with show action in Rails 4

I have and app with a users controller and I want to have it as a toplevel path in my routes, like:
get ':id' => 'users#show', as: :user_profile
and my to_param method in User is:
def to_param
self.username
end
So that when you hit "/rodrigo" for example, it will look for the User object with the username = "rodrigo". So far, so good.
But I also have some static pages that I want to have toplevel paths as well, such as about, terms,
controller :home do
get 'about', to: :about, as: 'about'
get 'help', to: :help, as: 'help'
get 'terms', to: :terms, as: 'terms'
get 'privacy', to: :privacy, as: 'privacy'
end
what happens is that when I try to access any of these static pages I get:
NoMethodError in Users#show
Showing /Users/rodrigovieira/Code/golaco/app/views/users/show.html.erb where line #1 raised:
undefined method `name' for nil:NilClass
Also, my users#show routes is defined before the static pages routes in routes.rb.
that is, Rails thinks I'm talking about a user object. How can I circumvent this problem?
I'm pretty sure it's possible. I appreciate any help.
Rails routes are matched in the order they are specified, so if you have a resources :photos above a get 'photos/poll' the show action's route for the resources line will be matched before the get line. To fix this, move the get line above the resources line so that it is matched first.
Golaco::Application.routes.draw do
# Institutional/Static pages
controller :home do
get 'about', to: :about, as: 'about'
get 'help', to: :help, as: 'help'
get 'terms', to: :terms, as: 'terms'
get 'privacy', to: :privacy, as: 'privacy'
end
get ':id' => 'users#show', as: :user_profile
resources :users, path: "/", only: [:edit, :update]
devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }
root 'home#index'
end

Abstracting rails route

I want to replace the normal /users/:id route that is created by the resources command, with a more abstract /profile route. It won't be possible to view other users profiles in my app, and therefor the current route is unnecessary specific.
I have tried to overwrite the route created by 'resources :users' with:
get '/profile', to: 'users#show'
and other variances and combinations, but can't seem to get it right. Either the controller can't find the user because of a missing id or it simply can't find the route.
Thanks for the help!
You can use this code in routes.rb file:
resources :users, :except => :show
collection do
get 'profile', :action => 'show'
end
end
It will generate url "/users/profile".
But, if u want to use only '/profile', then don't create route as collection inside users resources block.
resources :users, :except => :show
get 'profile' => "users#show", :as => :user_profile
It will redirect '/profile' to show action in users controller.
I suggest simply adding a users/me route pointing to the show action of your UsersController like so:
resources :users, only: [] do
collection do
get 'me', action: :show
end
end
You can also use the match keyword in routes.rb file.
match 'users/:id' => 'users#show', as: :user_profile, via: :get

Not getting a [:id] parameter from url, Default route is not working

When I request a URL as follows:
http://localhost:3000/password_resets/edit/4RghIKJNygEDswIuuCo
I'm not getting the [:id] parameter, i.e. 4RghIKJNygEDswIuuCo.
Here is my route file, are there any modifications required for this?
ActionController::Routing::Routes.draw do |map|
match 'primary', :to => 'pages#primary', :as => "primary"
match 'admins', :to => 'admin_users#list', :as => "admins"
match 'login', :to => 'user_sessions#new', :as => "login"
match 'logout', :to => 'user_sessions#destroy', :as => "logout"
root :to =>"public#index"
match 'HFA/:id/' => 'public#show'
match 'HFA/:id/:uid' =>'public#show'
match 'public/projectview/:projectid/' => 'public#projectview'
map.connect ':controller/:action/:id.:format'
map.connect ':controller/:action/:id'
resources :users
resources :usertypes
resources :user_sessions
end
The default is /controller/:id/edit, following the REST architecture.
Do you really want to change this?
If so, verify the order of the declarations:
Rails routes are matched in the order they are specified, so if you
have a resources :photos above a get 'photos/poll' the show action’s
route for the resources line will be matched before the get line. To
fix this, move the get line above the resources line so that it is
matched first.
http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions
and check your availables routes with the rake routes command:
http://guides.rubyonrails.org/routing.html#inspecting-and-testing-routes
if you put match instead of 'get' you will get all http (GET, POST, PUT, DELETE) route verbs
get 'password_resets/:id/edit', to: 'password_reset#create', as: :send_password_reset
the 'to:' defines the controller method, 'as:' defines the path name, 'get' defines the route verb and ':id' is the token created by this line in app/views/user_mailer/password_reset.text.erb
<%= send_password_reset_url(#user.password_reset_token) %>
run "rake routes" command and check the routes for "password_resets/edit/4RghIKJNygEDswIuuCo".

Resources