Rails - exclude some url form routing? - ruby-on-rails

I dont know how to tell this. I have my site on url like:
www.mysite.com
I want to have some other "stuff" on url:
www.mysite.com/some/mystuff
How can i hm exclude this url from rails app? When i put this url in browser it tells me that there is no such page. Is this possible?

Inside config/routes.rb, you could add:
match "*missing" => redirect("/")
This will redirect any unknown urls directly to the homepage. Also, you could create the famous Error 404 page and redirect to it.
match "*missing" => 'application#404'
ApplicationController:
def 404
render: 'home/404'
end

Related

How to implement user specific sub domains in rails

I am writing a rails application where users can have their own username based subdomain like GitHub pages => USERNAME.github.io
What I have done is created a controller which parses subdomain from the request and finds the corresponding user.
def show
#user = User.where(name: request.subdomain)
end
now how should I write my route so that I can accept any user specific subdomain and direct it to the above controller
Yes, you can get the subdomain in your routes and direct to your controller.
You will get your subdomain either with a helper or in your routes with regexp. a regexp example is below
get '/', to: 'controller#show', constraints: { subdomain: '/^[a-zA-Z]*/' }
the above example should get a typical subdomain.
you can find some good examples here in the links below. The rails cast has a very good example of routing by subdomain
http://guides.rubyonrails.org/routing.html
http://railscasts.com/episodes/221-subdomains-in-rails-3

How can I route to a page any time no route is available?

I want to make a simple page with a back button for the odd case that a user enters a url to a page there is not route for.
For instance, the route for foo is:
resources :foos, :except => [:index]
The user enters:
mysite.com/foos
I want to display a page that says "This page doesn't exist" and a back button.
Where do I put the html.erb file and how to I account for that in routes.rb?
Thanks
At the end of your routes.rb write:
match '*path', :controller => 'some_controller', :action => 'some_action'
or
match '*path' => 'some_controller#some_action'
Source:
rails handle 404 with url redirect
In the production mode, the 404.html in the /public folder of your Rails Application will be renderd for a Routing Error instead of displaying the error message.
As per my knowledge you have two ways to do this:
If you would like to capture errors like (404,500..etc.,) use rescue_from in ActionController. Otherwise if you just want to edit the default error pages, edit the 500.html and 404.html files in {Rails.root}/public
Example: How to properly render custom 404 and 500 pages?
2.Custom Error Page - Ruby on Rails
When a client's request can not be found in the server, a 404 redirect is made. To costumize the 404 page, simply change it in public/404.html

How to redirect a URL with GET variables in routes.rb without Rails stripping out the variable first?

I am building a website in Rails to replace an existing website. In routes.rb I am trying to redirect some of the old URLs to their new equivalents (some of the URL slugs are changing so a dynamic solution is not possible.)
My routes.rb looks like this:
match "/index.php?page=contact-us" => redirect("/contact-us")
match "/index.php?page=about-us" => redirect("/about-us")
match "/index.php?page=committees" => redirect("/teams")
When I visit /index.php?page=contact-us I am not redirected to /contact-us. I have determined this is because Rails is removing the get variables and only trying to match /index.php. For example, If I pass /index.php?page=contact-us into the below routes I will be redirected to /foobar:
match "/index.php?page=contact-us" => redirect("/contact-us")
match "/index.php?page=about-us" => redirect("/about-us")
match "/index.php?page=committees" => redirect("/teams")
match "/index.php" => redirect("/foobar")
How can I keep the GET variables in the string and redirect the old URLs the way I'd like? Does Rails have an intended mechanism for this?
I had a similar situation, this is what I did:
Create a controller taking care of redirects with one action
RedirectController < ApplicationController
def redirect_url
if params[:page]
redirect_to "/#{params[:page]}", :status => 301
else
raise 404 #how handle your 404
end
end
In routes.rb
match "/index.php" => "redirect#redirect_url"

How can I redirect multiple variants to the same URL?

When using match in rails routing, as:
match "/about/" => about#index
both mysite.com/about/ and mysite.com/about show the same page.
This may create some duplicate content issues.
What I what to accomplish is that mysite.com/about will be directed to the other URL varient, i.e. mysite.com/about/.
I tried to accomplish simple by writing:
match "/about" => "/about"
but this cause a major error in my routing file and the application failed.
Obviously I'm doing something wrong.
Please advise.
use the following line in application.rb to generate a tailing slash for all the url
config.action_controller.default_url_options = { :trailing_slash => true }

Rails 3 Routing to Add String to Home URL

Using Rails 3 routing, I'd like all visitors to the home page
http//mysite.com/
to see the URL
http//mysite.com/mykeyword
in their browser URL.
In other words, all visits to the home page get redirected to
http//mysite.com/mykeyword
This is approximately the opposite of the typical route
root :to => "home#index"
or
match '/' => 'home#index'
where any controller name gets stripped off from the home page URL.
Instead I want every visit to the site home page to include "mykeyword" in the URL.
How?
Justin is correct, but I believe you also want a redirect. To do that:
match "/" => redirect("/mykeyword")
Try this:
match '/mykeyword' => "home#index"
You want visitors to still be on the homepage, but the url to say "www.mysite.com/mykeyword" right? If so, that should work.

Resources