Make Rails ignore route if URL doesn't match a Regex - ruby-on-rails

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?

Related

Ruby on Rails Routing for Wildcard URL

I was trying to pull a segment from the URL and since I'm a rookie, kind of confused by the routing.
My code to get the last segment works fine, it's my routing that is messed up.
Ideally the URL would like this:
http://localhost.com/track/item/1234
I'm using Rails v4.0.8 & Ruby v2.0.0p451
The error is No route matches [GET] "/track/item/1234"
Here's the whole routes.rb file:
SepContact::Application.routes.draw do
get "track/item"
get "track/item/:id"
get "contacts/contact"
resources "contacts", only: [:new, :create]
end
I think CWitty's should work as well but here is a more explicit format.
match "track/items", to: 'controller#index', via: :get, as: "items"
match "track/items/:id", to: 'controller#show', via: :get, as: "item"
Note I updated your url to be more rails like items rather than item
I think most of your problem is with the track segment of the url.
I don't see how get 'track/items' would map the the items#index controller / method
I think the match method would be needed here to explicitly map the url to the correct controller and method.
Is there a good reason you are naming you url like that?
You can read all about routing here:
http://guides.rubyonrails.org/routing.html
Here is the section of the above document that discusses using the match method:
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
Routing both GET and POST requests to a single action has security implications. In general, you should avoid routing all verbs to an action unless you have a good reason to.
Your routes should be like:
SepContact::Application.routes.draw do
get "track/item/:id", to: 'controller#action'
get "track/item", to: 'controller#action'
get "contacts/contact" to: 'controller#action'
resources :contacts, only: [:new, :create]
end
You need to specify a to: pointing to a controller and action unless you use the resource or resources helper.

Change url for a route in Rails

I have a search scope for my users with the following route:
resources :users do
collection do
get :search
end
end
This however generates /users/search as url. I would like to have /search as url. I tried the following:
get '/search', as: :search
get '/search' => 'users#search', as: :search
get :search, to: 'users#search', as: :search
They don't seem to work since I keep getting routing errors. What would be the correct way to write it?
This one should work (without the leading '/') :
resources :users
get 'search' => 'users#search', as: :search
The named helpers for this route will be search_path and search_url
you can also use match:
match "/search", to: "users#search", via: "get"

Ruby on Rails routing for variable controller

I have a URL I want to be able to redirect to.
Something similar to:
"http://localhost:3000/username/admin/page".
I have a match in routes.rb as:
match ':account/admin/:page' => "admin#index"
I have redirect code:
redirect_to :controller => account.username, :action=>"admin", :page=>"index"
This, however comes up with a routing error:
No route matches {:action=>"admin", :controller=>"sdunn", :page=>"index"}
I know what I have done is wrong, but how can I fix this?
Many thanks.
Route is expecting 2 parameters, first one is :account, second is :page, i think you are only passing :page. I would add :as => 'some_name' to your route and then use _path :
routes.rb
match ':account/admin/:page' => "admin#index", :as => 'my_route'
controller:
redirect_to my_route_path(#user, #page)
my_route_path could be something different depending on your exact route file, so use
rake routes | grep my_route
to see exact name, then add _path to the end.

Rails Routes Question

I have a really simple app I've built using RoR but I'm stuck modifying my routes.
It's basically a site which lists user information - I need to change the url from:
mydomain.com/users/user-1
to
mydomain.com/user-1
Update..
I've managed to route the above request using:
match "/:id", :controller=>"users", :action=>"show"
But what I really need to do is change the route for all requests to /users/# to /
Although my route is working, all my links to show a user still point to:
/users/user-#
--- Update ---
The routing for /user-id is now working perfectly however, I'm struggling with the rest of the routing now.
I can now navigate to http://localhost/user-1
However, I basically need to remove the /user/ part completely. When I'm editing / updating a page, I end up with it going to:
/users/user-1/edit
All works fine but it then redirects to"
/users/user-1/
I really need both of these to redirect to
http://localhost/user-1/edit
Thanks
Bob
You want:
resources :users, :path => '/'
I believe get ":id" => "users#show" will be much the same except you only allow HTTP GET. Hope this works.
At the bottom of your routes
match "/:id", :to => "users#show"
There is some side effects so be ready
to rewrite your routes you should specify its name:
match "/:id", :to => "users#show", :as => :user
or, as #Whirlwin pointed, better to use just GET request as default
get "/:id", :to => "users#show", :as => :user
So now you can call:
user_path(#user)

Ruby on Rails: subdomain too powerful? how do I set this up the right way

routes:
match '/' => 'site_admin/admin#index'
resources :link_pages
resources :services
resource :user_session
resource :account, :controller => "users"
resources :password_resets
resources :users
resources :addresses
resources :info
match "/home", :to => 'info#home'
match "/register", :to => 'users#new'
root :to => 'info#home'
match ':controller(/:action(/:id(.:format)))'
so when I got to admin.lvh.me:3000/ it goes to site_admin/admin#index... which is great...
but when I take off the subdomain, and just have lvh.me:3000/ it goes to the same route....
how do I get admin to stay where it is. and no subdomain to go to my root page, as in my routes file?
Routes are parsed in order, so when you request / from any domain it finds "match '/'..." first and sends you to the specified page. Your subdomain isn't coming into play at all. You can use Request-based constraints to route based on subdomain:
http://guides.rubyonrails.org/routing.html#request-based-constraints
Not sure how subdomain factors into this at all. Perhaps you're confusing subdomain with route namespacing (http://edgeguides.rubyonrails.org/routing.html#controller-namespaces-and-routing)?
match '/' => 'site_admin/admin#index'
Is being selected over
root :to => 'info#home'
Because it's defined first in the routes file. They're ostensibly the same thing.
Yes #Cory is right. Above both statements are similar and first defined route is considered every time. If you change admin route to
match '/admin' => 'site_admin/admin#index'
then it does make sense... What say??
or else, using the following code you can determine your URL conditionally:
request.subdomains(0).first will give you the subdomain value- either admin or blank. But it will go to any one controller action only which is defined first in route.rb file.
Then from that action using subdomain, you can decide where to re-direct it- either to admin panel or home page...

Resources