Rails 3 Routing to Add String to Home URL - ruby-on-rails

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.

Related

How does redirect work in rails?

I don't have a /charge page. But I do have a subscription/charge page. I was wondering, how do I say, "if a user goes to /charge, redirect them to subscription/charge" using routes
No route matches [POST] "/charged"
You can modify your routes.rb file to point a url to a specific action
get '/charge', to: 'subscription#charge'
I'm using something similar to below in my application for post:
match '/charge' => 'subscription#charge', :via => [:post], :as => :subscription_charge
you can also write:
root 'subscription#charge'
and you'll be redirected to the charge page if accessing http://localhost:3000

Rails - exclude some url form routing?

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

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 route "/" to public/index.html in routes.rb?

My Facebook app uses a AS3 front end and a APIish Rails backend.
Facebook requires your "canvas page" (your app page sucked into the Facebook chrome) either be dynamic (index.erb) or a directory (end with "/"). Since I don't know Rails views, I went with the url of "myapp.herokuapps.com/".
Heroku logs give this error:
ActionController::RoutingError (No route matches "/")
How can I match "/" to the index file?
match "/" => ????
I am used to routing resources, but not static pages.
If you want to display a static page as your root, you should be using the High Voltage gem. This allows you to add static pages to your site as follows:
Add gem 'high_voltage' to your Gemfile
Put your static home.html.erb page in /app/views/pages/
Route to your static page with root to: => 'high_voltage/pages#show', :id => 'home'
No redirects are necessary.
The High Voltage docs can be found at https://github.com/thoughtbot/high_voltage.
You need to define a root_path in your routes.rb file
So root :to => 'YOUR_CONTROLLER_NAME#YOUR_CONTROLLER_ACTION' should do it.
If you wanted to map it to a file, then just give the name of the file in the public directory, so if you wanted to map public/index.html to root then this would work:
root :to => 'index'
Both answers are correct; however, the folks at thoughtbot recommend the following:
Under "config/initializers/" create the file "high_voltage.rb"
Write in the following code
HighVoltage.configure do |config|
config.home_page = "home"
end
Restart your server
You can check the documentation at https://github.com/thoughtbot/high_voltage#specifying-a-root-path

rails 3 - one app, multiple domains, how implement a different 'root' route for one of the domains?

Several different domains names point to my app on heroku, for example foo.com and bar.com both point to the app. (We host specialized blog pages, and foo.com is the domain used by our Users who are creating web pages, and bar.com is the 'public facing' domain where the blog pages are.)
All the user-editing pages have Devise authentication, and the "root" on foo.com is the User's dashboard page. And a logged-in user can preview their blog page at foo.com/reviewpage/USERID
each User acount also has a unique "friendly url name" such as "acme-inc-dallas-tx"
On the public-facing web page bar.com (but ONLY this one domain), I need to somehow map
http://bar.com/friendly-url-name to :controller => mycontroller, :action => myaction
I assume that means I need to re-map 'root' on bar.com (but ONLY bar.com) to a method "find_friendly_url" that looks up the appropriate page and displays it.
If that is the right way to proceed... how would I remap 'root' for one and only one domain that points to my app?
I usually proceed as follows:
constraints(Subdomain) do
match "/" => "home#admin"
end
match "/" => "home#standard"
Or:
match "/" => "home#admin", :constraints => {:subdomain => "admin"}
match "/" => "home#standard"
Which creates:
/(.:format) {:action=>"admin", :subdomain=>"admin", :controller=>"home"}
root /(.:format) {:action=>"standard", :controller=>"home"}
The same logic lets you create routes only available to desired subdomains.

Resources