I am trying to set up some semistatic page in a rails 3 app,
I have created a Pages controller with some non restful actions
class PagesController < ApplicationController
def home
end
def about
end
def contact
end
def monday
end
def saturday
end
def sunday
end
end
it’s showing the pages well at pages/monday etc… or /monday etc… if i set up the routes as is rails 3 removing controller name form url
But I was just wondering if it is possible to redirect a missing url to /. As per exampel i have /monday set up, but if one plays with the url and input /tuesday it won’t hit a page. can i redirect this kind of actions?
==EDIT==
I’ve change my code to this:
resources :pages, :path => '/' do
collection do
# # match 'tuesday' => redirect('/')
# # match 'wednesday' => redirect('/')
# # match 'friday' => redirect('/')
%w{home monday thursday saturday sunday about contact resources}.each do |url|
get url
end
end
end
match 'pages/*page' => :root
the url rewriting works fine looping through the array.
I tried the put the wild cardline at the bottom of my root files and at the bottom of the resources :page block. But I get a “controller action show could not be found” message.
match 'pages/*page' => :root
IS that the correct place to put it to restrain missing url like /tuesday to generate an error page?
is it also possible to limit this redirection only to a few actions and not to all missing pages? ex only to tuesday, wednesday, friday…
You can use wildcard routes to do this. At the end of your routes.rb just add the line:
and the missing pages will redirect to root with params[:page] set to the url requested.
If you want to only redirect missing pages in the Pages controller, do this instead
You can read more about routing here. Wildcard routes are explained near the end of the article.
Hope this helped!