I'm working on creating an app that allows users to create surveys. I decided to add in the rapidfire gem which really helps to speed up the process of making surveys.
So far it's working great, however the "Home" link in my head that points to the root_url no longer brings me back to the root, but the "rapidfire" homepage.
Here's my config/routes.rb file:
SurveyMe::Application.routes.draw do
devise_for :developers
devise_for :users
mount Rapidfire::Engine => "/surveys"
root "static_pages#home"
match "/about", to: "static_pages#use", via: "get"
match "/developers", to: "static_pages#developer" , via: "get"
match "/how", to: "static_pages#how", via: "get"
when I do rake routes I get this:
rapidfire /surveys Rapidfire::Engine
root GET / static_pages#home
about GET /about(.:format) static_pages#use
developers GET /developers(.:format) static_pages#developer
how GET /how(.:format) static_pages#how
Routes for Rapidfire::Engine:
results_question_group GET /question_groups/:id/results(.:format) rapidfire/question_groups#results
question_group_questions GET /question_groups/:question_group_id/questions(.:format) rapidfire/questions#index
POST /question_groups/:question_group_id/questions(.:format) rapidfire/questions#create
new_question_group_question GET /question_groups/:question_group_id/questions/new(.:format) rapidfire/questions#new
edit_question_group_question GET /question_groups/:question_group_id/questions/:id/edit(.:format) rapidfire/questions#edit
question_group_question GET /question_groups/:question_group_id/questions/:id(.:format) rapidfire/questions#show
PATCH /question_groups/:question_group_id/questions/:id(.:format) rapidfire/questions#update
PUT /question_groups/:question_group_id/questions/:id(.:format) rapidfire/questions#update
DELETE /question_groups/:question_group_id/questions/:id(.:format) rapidfire/questions#destroy
question_group_answer_groups POST /question_groups/:question_group_id/answer_groups(.:format) rapidfire/answer_groups#create
new_question_group_answer_group GET /question_groups/:question_group_id/answer_groups/new(.:format) rapidfire/answer_groups#new
question_groups GET /question_groups(.:format) rapidfire/question_groups#index
POST /question_groups(.:format) rapidfire/question_groups#create
new_question_group GET /question_groups/new(.:format) rapidfire/question_groups#new
edit_question_group GET /question_groups/:id/edit(.:format) rapidfire/question_groups#edit
question_group GET /question_groups/:id(.:format) rapidfire/question_groups#show
PATCH /question_groups/:id(.:format) rapidfire/question_groups#update
PUT /question_groups/:id(.:format) rapidfire/question_groups#update
DELETE /question_groups/:id(.:format) rapidfire/question_groups#destroy
root GET / rapidfire/question_groups#index
I guess I don't really understanding what the "mount Rapidfire::Engine => "/surveys"" line is doing. Is there anyway I can adjust the root that it's generating so that it indeed points back to my original app's home page?
RapidFire, as an engine, has it's own root which is mounted under the /surveys path that you specified. Calling root_path from within a RapidFire namespaced controller/view will refer to the that root - not the sitewide root.
To fix this, you can pass the sitewide root path to the engine, and call that from the header instead - see Rails.root from engine for a way to do this.
Related
I know how to change routes when I'm calling some controller which name is in URL, but do not have idea what do with this:
Will_paginate:
http://localhost:3000/?page=4
and I want
http://localhost:3000/page/4
Route:
root to: 'pages#home_logged_in'
I'm not sure if will_paginate has the functionality to change the way it structures the links so you may need to calculate and display the page links manually in your views.
In the routes, you can setup a standard non-paginated route to handle the initial request and then a paginated route to handle individual pages.
get '/pages/:page', to: 'pages#index' # will pass params[:page]
root to: 'pages#index'
Then in the pages#index you just check for and apply the page param if it exists:
def index
page = params[:page] || 1
#items_being_paginated = ItemModel.paginate(page)
end
I've not used the will_paginate gem personally so the gem specific code is taken from their wiki.
Depending on what you're using this for -- if its just paginating items, for example -- the above should work for you. If you're trying to get SEO quality links for individual items you can using dynamic routing, like this:
get '*path', to: 'pages#show', as: 'pages_show'
or parameterized
get ':category/:sub_category', to: 'pages#index', as: 'sub_pages_index'
get ':category/:sub_category/pages/:page', to: 'pages#index', as: 'sub_paginated_pages_index'
get ':category/:sub_category/:item', to: 'pages#show', as: 'pages_show'
In the controller you just parse the route with the first, or use the params with the second.
This part of the Rails Routing Guide might interest you if you want more info.
I have a large number of routes that I would like to separate into different route files.
I created a "routes-secondary.rb" and added some routes there. I then tried to add something like this in the app's main routes.rb:
require "#{Rails.root}/config/routes-secondary.rb"
That does not work however because Rails doesn't recognize the routes in routes-secondary.rb. Is there a way to do this correctly?
(I've updated this answer to take advantage of the RouteReloader for development work)
You can easily accomplish this (even in Rails 4!).
config/routes.rb:
Rails.application.routes.draw do
resources :foo
end
config/routes/included.rb:
Rails.application.routes.draw do
resources :bar
end
config/initializers/routes.rb
Rails.application.routes_reloader.paths.unshift *Dir[File.expand_path("../../routes/**/*.rb", __FILE__)]
This will add all files under config/routes to the application routes, and it'll probably add them in reverse lexical order by filename. If you want to load the routes in a different order, rather than the glob, you can just push or unshift the routes onto routes_reloader.paths in the order desired.
rake routes:
Prefix Verb URI Pattern Controller#Action
foo_index GET /foo(.:format) foo#index
POST /foo(.:format) foo#create
new_foo GET /foo/new(.:format) foo#new
edit_foo GET /foo/:id/edit(.:format) foo#edit
foo GET /foo/:id(.:format) foo#show
PATCH /foo/:id(.:format) foo#update
PUT /foo/:id(.:format) foo#update
DELETE /foo/:id(.:format) foo#destroy
bar_index GET /bar(.:format) bar#index
POST /bar(.:format) bar#create
new_bar GET /bar/new(.:format) bar#new
edit_bar GET /bar/:id/edit(.:format) bar#edit
bar GET /bar/:id(.:format) bar#show
PATCH /bar/:id(.:format) bar#update
PUT /bar/:id(.:format) bar#update
DELETE /bar/:id(.:format) bar#destroy
If you are using Rails 4, you cannot do this out of the box and it's explained here. In rails 3, you can modify your config.paths hash as explained here.
A few questions about this route:
root :to => "controller#method"
When a GET request is made in what part of Rails does it equate GET "/" with root?
What is root (i.e. a variable, method, other)?
root as well as match define verb-independent routes, meaning all http verbs are included in the routing rule. If you want to define verb-dependant rules, you can use get/post/put/delete rules like:
get 'profile', to: 'users#show'
In particular, root defines what the default page (when you acess the root path of your application) is.
You can find all the information you need here:
http://guides.rubyonrails.org/routing.html
I have a Rails app that I am trying to integrate a Rails engine in to.
The host app has some catch all routes:
# magic urls
match '/' => 'admin/rendering#show'
match '*path/edit' => 'admin/rendering#show', :defaults => { :editing => true }
match '*path' => 'admin/rendering#show'
It looks like the engine routes are loaded after the application catches all routes.
/sitemap.xml(.:format) {:format=>"xml", :controller=>"admin/sitemaps", :action=>"show"}
/(.:format) {:controller=>"admin/rendering", :action=>"show"}
/*path/edit(.:format) {:controller=>"admin/rendering", :action=>"show"}
/*path {:controller=>"admin/rendering", :action=>"show"}
engine_envs GET /engine/envs/:id(.:format) {:controller=>"engine/envs", :action=>"show"}
PUT /engine/envs/:id(.:format) {:controller=>"engine/envs", :action=>"update"}
jammit /assets/:package.:extension(.:format) {:extension=>/.+/, :controller=>"jammit", :action=>"package"}
So far, everything is hitting the /engine/envs routes are getting caught by the application catch all routes. However I see that the jammit route is loaded after the engine and I don't believe those are getting caught. Any way to override the app routes?
You could stick your engine routes in a method and then call that in your host app.
# engine routes.rb
module ActionDispatch::Routing
class Mapper
def engine_routes
engine_envs GET /engine/envs/:id(.:format)
# ...
end
# ...
and then in your host app add the method before the catch-all route
# host app routes.rb
MyTestApp::Application.routes.draw do
# ...
engine_routes
match '/' => 'admin/rendering#show'
match '*path/edit' => 'admin/rendering#show', :defaults => { :editing => true }
match '*path' => 'admin/rendering#show'
end
Routes are used in the order they are defined. The first routes to be read are the one of the host application, then of your engine.
As soon as a matching route is found, the search for a route is stopped.
As far as I know, there are no way (I may be wrong about this) to override this feature other than to change your "mag
UPDATE: So that means that the order you see them in "rake routes" is the order they are processed. As soon as a matching route is found, there you go.
I have a Rails app that has a controller called domain which has a nested controller called subdomain and stats. I have defined them in routes.rb:
resources :domains do
resources :subdomains, :stats
end
I have changed the to_param of the domain and subdomain models to use the name of the domain, e.g.: the routing I get is http://site/domains/foo/subdomains/bar.
I would like to tidy it up to so that instead of using http://site/domains/foo/subdomains/bar I could access it with just http://site/foo/subdomains/bar. I have tried the following in routes.rb:
match "/:id/" => "domains#show", :as => :domain
Which works fine, but it only gives me the ability to use the path http://site/foo but for example http://site/foo/subdomains/bar doesn't. I could create match lines for every respective model and nested model but that does nothing to other helpers besides domain_url - i.e. edit_domain_url points to /domains/foo/edit/ instead of /foo/edit.
Is there a way to change the routing so that the resources generates helpers that point to the root url without the 'domains' part?
The single match in your routes creates only one route. Resource helpers create many routes at once. Luckily there are a lot of options for customisation. If you want to omit /domains/ from your paths, it's as simple as:
resources :domains, :path => "/" do
resources :subdomains, :stats
end
With the above in config/routes.rb, running rake routes says the following:
domain_subdomains GET /:domain_id/subdomains(.:format)
domain_subdomains POST /:domain_id/subdomains(.:format)
new_domain_subdomain GET /:domain_id/subdomains/new(.:format)
edit_domain_subdomain GET /:domain_id/subdomains/:id/edit(.:format)
domain_subdomain GET /:domain_id/subdomains/:id(.:format)
domain_subdomain PUT /:domain_id/subdomains/:id(.:format)
domain_subdomain DELETE /:domain_id/subdomains/:id(.:format)
domain_stats GET /:domain_id/stats(.:format)
domain_stats POST /:domain_id/stats(.:format)
new_domain_stat GET /:domain_id/stats/new(.:format)
edit_domain_stat GET /:domain_id/stats/:id/edit(.:format)
domain_stat GET /:domain_id/stats/:id(.:format)
domain_stat PUT /:domain_id/stats/:id(.:format)
domain_stat DELETE /:domain_id/stats/:id(.:format)
domains GET /(.:format)
domains POST /(.:format)
new_domain GET /new(.:format)
edit_domain GET /:id/edit(.:format)
domain GET /:id(.:format)
domain PUT /:id(.:format)
domain DELETE /:id(.:format)
Looks like all the routes you need!