Typus route order - ruby-on-rails

It used to be that you could load Typus routes exactly where you needed them by placing
Typus::Routes.draw(map)
at the appropriate point in your routes.rb file. It seems that this is no longer supported and that they're always loaded after all of the application routes. This causes problems with catchall routes which must be defined last. Does anyone know how to control the load order for typus now? Is there a way to get them defined before any of the app routes rather than after? Thanks!

I got around it by leaving my catch-all routes at the end of my apps routes.rb BUT excluding it from matching for Typus urls:
# A catch all route
match '*path' => 'content#show', :constraints => lambda{|request|
!request.path.starts_with?("/admin") # excluded if typus will be taking it...
}
This may or may now work for you...

I'm looking for the same answer.
At the moment I have resorted to copying the contents from typus's config/routes.rb and placing it into my routes.rb file, before the catchall route.
It's a horrible, hackish solution, but it's solving my immediate problem.
Example:
# TODO: KLUDGE: MANUALLY BRING THE TYPUS ROUTES IN
# Typus used to provide :
# Typus::Routes.draw(map)
# But that is no longer the case.
scope "admin", :module => :admin, :as => "admin" do
match "/" => "dashboard#show", :as => "dashboard"
match "user_guide" => "base#user_guide"
if Typus.authentication == :session
resource :session, :only => [:new, :create, :destroy], :controller => :session
resources :account, :only => [:new, :create, :show, :forgot_password] do
collection do
get :forgot_password
post :send_password
end
end
end
Typus.models.map { |i| i.to_resource }.each do |resource|
match "#{resource}(/:action(/:id(.:format)))", :controller => resource
end
Typus.resources.map { |i| i.underscore }.each do |resource|
match "#{resource}(/:action(/:id(.:format)))", :controller => resource
end
end
# END KLUDGE
# Catch all to the state page handler
match '/:page' => 'pages#show', :as => 'page'

Related

Can't figure out how Rails route helper should look

I am working on an assignment which includes adding a feature to Typo.
rake routes shows:
admin_content /admin/content {:controller=>"admin/content", :action=>"index"}
/admin/content(/:action(/:id)) {:action=>nil, :id=>nil, :controller=>"admin/content"}
I need to create a route helper which matches the following RESTful route: /admin/content/edit/:id and an example of url is /admin/content/edit/1
But I can't figure out how to do it. I tried something like admin_content_path(edit,some_article) but it didn't work. (some_article is just an article object)
In routes.rb file:
# some other code
# Admin/XController
%w{advanced cache categories comments content profiles feedback general pages
resources sidebar textfilters themes trackbacks users settings tags redirects seo post_types }.each do |i|
match "/admin/#{i}", :to => "admin/#{i}#index", :format => false
match "/admin/#{i}(/:action(/:id))", :to => "admin/#{i}", :action => nil, :id => nil, :format => false
end
#some other code
Thanks a lot for your help!
If you are using RESTful routes, why not use the Rails default routes?
So your routes.rb would look like
namespace :admin do
resources :content
resources :advanced
resources :categories
resources :comments
...
<etc>
end
This does assume all your controllers are in the folder admin (but from your comment this seems to be the case.
If you do that, you can just use the standard route-helper: edit_admin_content_path.
If you want to do it manually, you should try adding a name to your route. E.g. as follows:
match "/admin/#{i}/:action(/:id)" => "admin/#{i}", :as => "admin_#{i}_with_action"
and then you should do something like
admin_content_with_action(:action => 'edit', :id => whatevvvva)
As a side-note: I really do not like the meta-programming in your config/routes.rb, if for whatever you really find that the default resources are not a right fit, I would advise to use methods instead (as explained here)
So for example in your config/routes.rb you would write:
def add_my_resource(resource_name)
match "/#{resource_name}", :to => "#{resource_name}#index", :format => false
match "/#{resource_name}(/:action(/:id))", :to => "#{resource_name}", :as => 'admin_#{resource_name}_with_action", :action => nil, :id => nil, :format => false
end
namespace :admin do
add_my_resource :content
add_my_resource :advanced
add_my_resource :categories
...
end
which imho is much more readable.
But my advice, unless you really-really need to avoid it, would be to use the standard resources since you do not seem to add anything special.
HTH.

No route matches "/"?

I know this seems like a very simple error but its coming from a complex process.
Im trying to upgrade an old rails 2 app to rails 3. In my routes.rb file, i have
root :to => "home#index"
And I also have a file 'app/controllers/home_controller.rb' and a 'app/views/home/index.html.erb' so i simply dont get what could be causing this error. Upgrading to rails 3 isnt easy.
( in the home_controller.rb, i have def index
end )
Any suggestions?
**UPDATE - FULL ROUTES FILE**
SpecimenTracker::Application.routes do
map.resources :users
map.resource :session
# The priority is based upon order of creation: first created -> highest priority.
# Sample of regular route:
# map.connect 'products/:id', :controller => 'catalog', :action => 'view'
# Keep in mind you can assign values other than :controller and :action
# Sample of named route:
# map.purchase 'products/:id/purchase', :controller => 'catalog', :action => 'purchase'
# This route can be invoked with purchase_url(:id => product.id)
# Sample resource route (maps HTTP verbs to controller actions automatically):
# map.resources :products
# Sample resource route with options:
# map.resources :products, :member => { :short => :get, :toggle => :post }, :collection => { :sold => :get }
# Sample resource route with sub-resources:
# map.resources :products, :has_many => [ :comments, :sales ], :has_one => :seller
# Sample resource route with more complex sub-resources
# map.resources :products do |products|
# products.resources :comments
# products.resources :sales, :collection => { :recent => :get }
# end
# Sample resource route within a namespace:
# map.namespace :admin do |admin|
# # Directs /admin/products/* to Admin::ProductsController (app/controllers/admin/products_controller.rb)
# admin.resources :products
# end
# You can have the root of your site routed with map.root -- just remember to delete public/index.html.
get "home/index"
root :to => "home#index"
# See how all your routes lay out with "rake routes"
# Install the default routes as the lowest priority.
# Note: These default routes make all actions in every controller accessible via GET requests. You should
# consider removing the them or commenting them out if you're using named routes and resources.
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end
The "map.connect", "map.resources" are old syntax
My Rails 3 routes.rb starts with
ApplicationName::Application.routes.draw do
Rails Routing from the Outside In: http://guides.rubyonrails.org/routing.html
Just to make sure, have you tried restarting Webrick (or whatever other server you're using). While this is very simple, it will always end up tripping you up :)
If that's not the problem, please post your log files (log/development.log).
Edit: Just seen the update to your post. Try removing the other (uncommented) lines in your routes file, line by line, until the problem is fixed.
remove the two map.connect statements. you don't need them in rails3.
and the ressources at the top should be just:
resources :users
resources :sessions
Try:
root :to => 'home#index'
Note single quotes. # has special meaning in a double quoted string in Ruby.

Rails Path Helper for Root should output "/"

In my routes file, I have a resource called products. The index action of the products resource is also my root path.
resources :products
root :to => "products#index"
When I use the helper method products_path (in a redirect or link), it returns "/products". But what I want is for it to return "/". I know it's the same page, but I want to keep my URL's consistent.
How can I fix this?
Thanks!
root :to => 'products#index', :as => :products
match '', :to => 'products#index', :as => :root # recreate named root_path, if you use it anywhere
This will need to appear below your resources :products as it does in your example above. This will override the products_path and products_url you'd get from resources. Run rake routes before and after to compare.
if you only want to change Index try excluding it first then defining it on its own like so:
resources :products, :except => [:index]
resources :products, :only => [:index], :path => '/'
root_path() should return /
I guess you could redefine the products_path to be the same as root_path (like in a helper file):
def products_path(*params)
root_path *params
end

Setting up restful routes as a total newb

I'm getting the following error:
Unknown action
No action responded to show. Actions: activate, destroy, index, org_deals, search, and suspend
Controller:
class Admin::HomepagesController < Admin::ApplicationController
def org_deals
#organization = Organization.find(:all)
end
Routes:
map.root :controller => 'main'
map.admin '/admin', :controller => 'admin/main'
map.namespace :admin do |admin|
admin.resources :organizations, :collection => {:search => :get}, :member => {:suspend => :get, :activate => :get}
To note: This is a controller inside of a controller.
Any idea why this is defaulting to show?
Update:
I updated what the routes syntax is. Read that article, and tried quite a few variations but its still adamantly looking for a show.
Firstly, it looks like your routes file has the wrong syntax. If you are trying to establish routes for nested resources, you'd do it like so:
map.resources :admin
admin.resources :organizations
end
This would give you paths such as:
/admin/
/admin/1
/admin/1/organizations
/admin/1/organizations/1
The mapping from route to controller/action is done via a Rails convention, where HTTP verbs are assigned in ways that are useful for the typical CRUD operations. For example:
/admin/1/organizations/1
would map to several actions in the OrganizationsController, distinguished by the type of verb:
/admin/1/organizations/1 # GET -> :action => :show
/admin/1/organizations/1 # PUT -> :action => :update
/admin/1/organizations/1 # DELETE -> :action => :destroy
"Show" is one of the seven standard resourceful actions that Rails gives you by default. You can exclude "show" with the directive :except => :show, or specify only the resourceful actions you want with :only => :update, for example.
I recommend you look at Rails Routing from the Outside In, which is a great introduction to this topic.
EDIT
I see I ignored the namespacing in my answer, sorry. How about this:
map.namespace(:admin) do |admin|
admin.resources :homepages, :member => { :org_deals => :get }
end
This will generate your org_deals action as a GET with an id parameter (for the organization). You also get a show route, along with the six other resourceful routes. rake routes shows this:
org_deals_admin_homepage GET /admin/homepages/:id/org_deals(.:format) {:controller=>"admin/homepages", :action=>"org_deals"}
Of course your homepages_controller.rb has to live in app/controllers/admin/
EDIT redux
Actually, you want organizations in the path, I'll bet, in which case:
map.namespace(:admin) do |admin|
admin.resources :organizations, :controller => :homepages, :member => { :org_deals => :get }
end
which gives you:
org_deals_admin_organization GET /admin/organizations/:id/org_deals(.:format) {:controller=>"admin/homepages", :action=>"org_deals"}
By specifying admin.resources ... you are telling Rails you want the seven default different routes in your application. If you do not want them, and only want the ones you specify, do not use .resources. Show is called because that's the default route called for a GET request with a path such as /admin/id when you have the default resources.

Aliasing a namespaced route in Rails

Given the following routes.rb file:
# Add Admin section routes
map.namespace :admin do |admin|
admin.resources :admin_users
admin.resources :admin_user_sessions, :as => :sessions
admin.resources :dashboard
# Authentication Elements
admin.login '/login', :controller => 'admin_user_sessions', :action => 'new'
admin.logout '/logout', :controller => 'admin_user_sessions', :action => 'destroy'
# Default is login page for admin_users
admin.root :controller => 'admin_user_sessions', :action => 'new'
end
Is it possible to alias the 'admin' section to something else without having to change every redirection and link_to in the application? The main reason is that it's something I'd like to be configurable on the fly and hopefully make it also a bit less easy to guess.
map.namespace method just sets some common options for routes inside its block. It uses with_options method:
# File actionpack/lib/action_controller/routing/route_set.rb, line 47
def namespace(name, options = {}, &block)
if options[:namespace]
with_options({:path_prefix => "#{options.delete(:path_prefix)}/#{name}", :name_prefix => "#{options.delete(:name_prefix)}#{name}_", :namespace => "#{options.delete(:namespace)}#{name}/" }.merge(options), &block)
else
with_options({:path_prefix => name, :name_prefix => "#{name}_", :namespace => "#{name}/" }.merge(options), &block)
end
end
So it is possible to use with_options method directly instead of namespace:
map.with_options(:path_prefix => "yournewprefix", :name_prefix => "admin_", :namespace => "admin/" ) do |admin|
admin.resources :admin_users
# ....
end
And you can continue to use routes the same way as before, but prefix will be "yournewprefix" instead of "admin"
admin_admin_users_path #=> /yournewprefix/admin_users
In order to create an alias to the namespace (calling one api_version for example, from another router address) you can do the following:
#routes.rb
%w(v1 v2).each do |api_version|
namespace api_version, api_version: api_version, module: :v1 do
resources :some_resource
#...
end
end
this will cause the routes /v1/some_resource and /v2/some_resource to get to the same controller. then you can use params[:api_version] to get the evrsion you need and respond accordingly.
Like in any other resource, :path seem to be working fine for me.
namespace :admin, :path => "myspace" do
resources : notice
resources :article do
resources :links , :path => "url"
end
end
end

Resources