My route file has the following in it for the root. what i want is when i click on say about it doesn't show me http://127.0.0.1:3000/home/about what i would like is either http://127.0.0.1:3000/about
or just the URL http://127.0.0.1:3000/ like root dose. I'm running rails 3 if you can post a snip it of a route file that will do this or give me a url to see it at that would be great.
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
root :to => "home#index"
# See how all your routes lay out with "rake routes"
well, if you have a controller home with an action about and want to call about directly, you can do something in your routes like match "/about" => "home#about" :as => :about
if you want to call the route, for example by using a link_to you can say it like this:
<%= link_to "Home", about_url %>
you can do rake:routes to see all the generated routes, you'll see one mapped to about with controller => home and action => about
Then, when you call http://localhost/about it will actually call the home controller's about action
you can read more material about routing here
Check out http://edgeguides.rubyonrails.org/routing.html for more details.
You should put the root route at the end of the file.
You need to delete the public/index.html file for the root route to take effect.
Make sure you have a home controller
Make sure you have a view called index in the views/home/ folder
Then http://127.0.0.1:3000/ will display your index action
If you want your root to go to the about action, also change the route file to root :to => "home#about". Of course make sure you have an about view.
Related
I have several static erb pages being served up in a ruby rails 4 site via the high voltage gem:
get '/about' => 'high_voltage/pages#show', id: 'about'
get '/contact' => 'high_voltage/pages#show', id: 'contact', :protocol => "https"
get '/privacy' => 'high_voltage/pages#show', id: 'privacy'
This all works well and good, except that the /contact route doesn't redirect or force SSL on, it is happy with whatever protocol is used.
I host the site on engine yard, attempting to put :force_ssl only or variants in the route line resulted in failed deployments - high voltage uses a slightly different set of arguments than normal routes so I suspect there is a conflict somewhere.
Anyone use highvoltage and SSL with rails 4 for select static pages (not the whole site)? Example routes line please.
You can achieve this by overriding the HighVoltage#PagesController see the override section of the documentation.
It might look something like this:
class PagesController < ApplicationController
include HighVoltage::StaticPage
before_filter :ensure_secure_page
private
def ensure_secure_page
if params[:id] == 'contact'
# check to make sure SSL is being use. Redirect to secure page if not.
end
end
end
Next disable the routes that HighVoltage provides:
# config/initializers/high_voltage.rb
HighVoltage.routes = false
Then in your application's routes file you'll need to set up a new route:
# config/routes.rb
get "/pages/*id" => 'pages#show', as: :page, format: false
I have a route which is constrained to only run when a subdomain is present, which looks something like this:
# Set the default root for when a subdomain is used.
match '', to: 'public::pages#show', constraints: lambda { |r| r.subdomain.present? && r.subdomain != 'app' }
# Set the default application route.
root :to => "pages#index"
I'm looking to put together some routing specs to test that this routes properly (testing the app manually shows that the route does in fact work just fine) however I'm coming up against a few issues.
My current shot at a routing test for this is simply to supply the entire domain, with subdomain as part of the get() call:
it "routes to public page on subdomain" do
get('http://somesubdomain.example.com/').should route_to("public::pages#show")
end
However this spec fails, where the router has directed to the root event, rather than the intended one.
<{"controller"=>"public::pages", "action"=>"show"}> expected but was
<{"controller"=>"pages", "action"=>"index"}>.
Can anyone suggest why this might not be working?
Sounds like your root :to => "pages#index" route is taking precedence, and the match ''... route is getting ignored. I would try two root routes and set your :constraint lambda on the first one:
# Set the default root for when a subdomain is used.
root to: 'public::pages#show',
constraints: lambda { |r| r.subdomain.present? && r.subdomain != 'app' }
# Set the default application route.
root :to => "pages#index"
See this blog post for more details about using constraints on your root route.
I've got urlManager rules:
...
'/<slug:(nasha-glavnaya-zagolovok)>' => 'page/page/show',
'/pages/<slug>' => 'page/page/show',
...
all fine.
When I enter to address
/nasha-glavnaya-zagolovok
it opens the page i need
But if I try to open page
/pages/nasha-glavnaya-zagolovok
it also opens the page I need
But content of pages are same.
How i can redirect (with 301 header) from route to pattern if any rule in urlManager mathced?
i cannot give you the complete answer, but you can work make it work like this...
you can use a rule that sends the request to an action which job is just to redirect using CController::redirect()
replace this
'/<slug:(nasha-glavnaya-zagolovok)>' => 'page/page/show',
'/pages/<slug>' => 'page/page/show',
with this
'/<slug:(nasha-glavnaya-zagolovok)>' => 'page/page/show',
'/pages/<slug>' => 'site/redirect',
and redirect the user to /$slug in the 'site/redirect' action
update: you can make it work by writing a new url rule which extends CBaseUrlRule, not sure if you care to take the time to do that if this is the only redirect you want to do though
Is there a way to set the default root routes in rails to be one of my controller action and showing in the url rather than the root "/" ?
Say I have a "Computer" controller with a "index" action.
When my user login to my application, I want the url to be
http://localhost:3000/computer/index rather than http://localhost:3000/
root :to => "computers#index"
does the latter one, how can I make the default root url to be something like the prior one ?
UPDATE: a better way would be
root :to => redirect(/path)
The simplest way would be to change which URL your users get re-directed to after they successfully log in.
You could also add a forced re-direct in your controller:
# routes.rb
root :to => "computers#force_redirect"
# computers_controller.rb
def force_redirect
redirect_to '/computers/index'
end
I am writing my first application in rails and here is what I did
C:\Personal\rails\demo>ruby -v
ruby 1.9.2p136 (2010-12-25) [i386-mingw32]
C:\Personal\rails\demo>rails -v
Rails 3.0.5
C:\Personal\rails\demo>rails generate model book
invoke active_record
create db/migrate/20110325190010_create_books.rb
create app/models/book.rb
invoke test_unit
create test/unit/book_test.rb
create test/fixtures/books.yml
C:\Personal\rails\demo>rake db:migrate
(in C:/Personal/rails/demo)
== CreateBooks: migrating ====================================================
-- create_table(:books)
-> 0.0000s
== CreateBooks: migrated (0.0000s) ===========================================
C:\Personal\rails\demo>rails generate controller admin
create app/controllers/admin_controller.rb
invoke erb
create app/views/admin
invoke test_unit
create test/functional/admin_controller_test.rb
invoke helper
create app/helpers/admin_helper.rb
invoke test_unit
create test/unit/helpers/admin_helper_test.rb
Then i edited the admin_controller.rb as follows:
class AdminController < ApplicationController
scaffold :book
end
Here is the routes.rb file
Demo::Application.routes.draw do
# The priority is based upon order of creation:
# first created -> highest priority.
# Sample of regular route:
# match 'products/:id' => 'catalog#view'
# Keep in mind you can assign values other than :controller and :action
# Sample of named route:
# match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
# This route can be invoked with purchase_url(:id => product.id)
# Sample resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Sample resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Sample resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Sample resource route with more complex sub-resources
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', :on => :collection
# end
# end
# Sample resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
# root :to => "welcome#index"
# See how all your routes lay out with "rake routes"
# This is a legacy wild controller route that's not recommended for RESTful applications.
# Note: This route will make all actions in every controller accessible via GET requests.
# match ':controller(/:action(/:id(.:format)))'
end
However, when I go to http://localhost:3000/admin, I get a "No route matches "/admin"" error. I noticed that my routes.rb has only the commented lines. Did I do something wrong?
You have not added a route for admin, why are all your routes commented out.
if admin is a resource add this line
resources :admin
Also in your controller you will need an index method and index view file because http://localhost:3000/admin will take you there
Try removing the comment from this line:
match ':controller(/:action(/:id(.:format)))'
Note: This answer is for the case when above mentioned points are met yet you keep seeing this error. It was Rails 3.2 and Ruby 2.3.8
Problem definition:
I successfully logged in using admin creds. When page landed on /admin I saw No route matches [GET] "/admin" error page. I hit my head everywhere and used all my knowledge. Also, generated routes and grep but no admin route was published.
$ rake routes | grep admin
but only devise related admin routes were published. And then I read the warning about the routes which I noticed later, which goes like
ActiveAdmin: ActiveAdmin::DatabaseHitDuringLoad: Your file, app/admin/account_tags.rb (line 4),
caused a database error while Active Admin was loading. This is most common
when your database is missing or doesn't have the latest migrations applied.
To prevent this error, move the code to a place where it will only be run
when a page is rendered. One solution can be, to wrap the query in a Proc.
Original error message: PG::UndefinedTable: ERROR: relation "account_tags" does not exist
Solution
I ran rake db:migrate and restarted the server, the problem was gone.