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
How to Install and Use Slim Template Engine with Middleman
How to Properly Test Cancan Abilities with Rspec
How to Pass Named Arguments to a Rake Task
Gemfile.Lock Always Has Changes Not Staged for Commit
How Does One Downgrade Jekyll to Work with Github Pages
Remove Element at Specific Index from Redis List
Rails 3/ Git/ Bundler Fatal Could Not Parse Object
How to Remotely Inspect the Data in My Rediscloud Dbs
How to Create a Dynamic Regular Expression in Ruby
Ruby Group Hashes by Value of Key
Logger.Debug Not Writing to Log File in Rails
Ssl Error on Http Post (Unknown Protocol)
How to Declare a Rake Task That Depends on a Parameterized Task
Ruby Yaml Parser by Passing Constructor
How to Iterate Over an Array of Arrays
Gem Install Rails Does Not Install Rails
Generate a 'Link_To' to the Controller Action 'Edit', Dynamically
Ruby on Rails Triggers Update on Serialized Attribute Every Time