Matching and Routes in Rails

Matching and Routes in Rails

match method has been deprecated.

Use get for GET and post for POST.

get '/about', to: 'static_pages#about'

Rails Routes: How to match Rails routes based on a pattern

I am not sure how you can handle as part of route. But you can write this code in another way. You can create a route that handle all such routes at the end of your primary route as below:

get '/:route'  => 'controller#route_for_all_views'

In your controller you should have this route_for_all_views action, which can handle all pages.

class SomeController < ApplicationController
def route_for_all_views
# handle your views and code with params[:route] here
end
end

Match request URL params with rails routes via recognize_path

You can accomplish that with the following snippet:

Rails.application.routes.routes.to_a.reduce(false) do |exists, route|
route.path.to_regexp.match?(path) || exists
end

But I do think the best choice is handling 404 errors using custom logic. Why doing what the router already does for you? Here's an example:

config.exceptions_app = self.routes
# This will route any exceptions caught to your router Rack app. Now you'll want to define routes to display those errors yourself:

# config/routes.rb
get "/404", :to => "errors#not_found"
get "/422", :to => "errors#unacceptable"
get "/500", :to => "errors#internal_error"

(Extracted from http://web.archive.org/web/20141231234828/http://wearestac.com/blog/dynamic-error-pages-in-rails)

Then you can do whatever logic you want to do on ErrorsController

Rails 4 match all routes for one controller

Why don't you just use the show action:

#config/routes.rb
resources :static, param: :page, only: :show #-> url.com/static/:page

#app/controllers/static_controller.rb
class StaticController < ApplicationController
def show
render "#{params[:page]}"
end
end

This way, you can pass the "page" directly through the link and have it all handled by Rails:

 <%= link_to "About", static_path("page") %>

Rails routes entry to match any controller action

You could use it as below:

get 'test/:action', controller: :test

This will create the route as below:

 GET    /test/:action(.:format)           test#:action

This will match any test controller actions (e.g. test/any_action)

Exact routing matching routes created by resources in Ruby on Rails

I think you wanted to know what the code should be in the routing file so...

match "users/:id", :to => "users#show", :via => :get, :as => :user
match "users", :to => "users#index", :via => :get, :as => :users
match "users", :to => "users#create", :via => :post
match "users/:id/edit", :to => "users#edit", :via => :get, :as => :edit_user
match "users/:id", :to => "users#update", :via => [:put, :patch]
match "users/new", :to => "users#new", :via => :get, :as => :new_user
match "users/:id", :to => "users#destroy", :via => :delete

Rails 4 - How to match routes in namespace

You simply wrap the routes you're declaring in a namespace like so:

namespace :login do
get 'index'
get 'logout'
end

http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing

get, match and resources in routes.rb

resources is a shortcut for generating seven routes needed for a REST interface.

resources :widgets is equivalent to writing

get    "widgets"          => "widgets#index",   :as => 'widgets'
get "widgets/:id" => "widgets#show", :as => 'widget'
get "widgets/new" => "widgets#new", :as => 'new_widget'
post "widgets" => "widgets#create", :as => 'widgets'
get "widgets/:id/edit" => "widgets#edit", :as => 'edit_widget'
patch "widgets/:id" => "widgets#update", :as => 'widget'
put "widgets/:id" => "widgets#update", :as => 'widget'
delete "widgets/:id" => "widgets#destroy", :as => 'widget'

it just saves you the trouble.

By the way, get is not exactly the same as match. get, post, put and delete are shortcuts for limiting the route to a single HTTP verb. The two route definitions below are equivalent.

match 'foo' => 'controller#action', :method => :get
get 'foo' => 'controller#action'

Why do my routes not match?

Root is the route when you visit localhost:3000, so when you visit 'localhost:3000' it is matched to static_pages/home in your case.

If you need 'localhost:3000/static_pages/home' also to be forwarded to the corresponding controller you need to define the route additionally like.

get 'static_pages/home'

Otherwise I don't think it is necessary to restart your server after changing your route, unless of course something is wrong, but mostly it does not resolve the issue.



Related Topics



Leave a reply



Submit