Difference between :as option in Rails 2 and Rails3 routing? - ruby-on-rails

In Rails 2.X we have:
map.resources :posts, :controller => 'posts', :as => 'articles'
This essentially creates an alias for our posts routes. For example, this sends "domain.com/articles/" to the posts controller index action.
In Rails3, however, the :as option behaves differently. For example:
resources :posts, :controller => 'posts', :as => 'articles'
sets a named route rather than an alias, and going to "domain.com/articles/" gives an error:
No route matches {:controller=>"posts"}
How do I get the old (Rails 2) :as behavior using the new (Rails 3) api?
PS: Please don't tell me to simply rename my controller. That's not an option for me.

From some cursory reading of the RoR guide on routing, I think you might need to try:
resources :articles, :controller => "posts"
(http://guides.rubyonrails.org/routing.html#specifying-a-controller-to-use)
You might also need to add :as => "articles", but that named helper might already be set up since you are adding :articles resources.

You can accomplish this same behavior using the path option:
resources :posts, :path => '/articles/'
Now for example /posts/new becomes /articles/new.

Related

Change resource route name

Hou could I change resource name without changing resource URL.
Explanation:
My resource route is resources :universities
So my routes are new_university_path or universities_path
Now I have changed my resource name to departments as follows:
resources :departments, :controller => :universities
But problem is when I run rake routes I found that my URL has already been changed. So I like a solution where after changing my resource name my routes are works as before, like new_university_path or universities_path
Is this possible in rails 3 ?
You can add:
resources :departments, :controller => :universities, :as => :universities
That should keep the URL helpers the same.
You should be able to do that by using naming routes by giving it the name you want for that route in :as option:
resources :departments, :controller => :universities, :as => :universities

In Rails, is map.resources now obsolete?

I was viewing an episode in RailsCasts and was looking at the source code for episode 145.
This was his code for the routes.rb file
ActionController::Routing::Routes.draw do |map|
map.resources :orders
map.current_cart 'cart', :controller => 'carts', :action => 'show', :id => 'current'
map.resources :line_items
map.resources :carts
map.resources :products
map.resources :categories
map.root :products
end
I was immediately thrown off. That looked like an entirely different syntax. Then I realized that this source code was published in 2010. I'm wondering if it's now obsolete, because I copied and pasted that code into my Rails Application and it isn't working.
Usually, what I do is
resources 'orders'
root 'products'
I don't know how I would rewrite map.current_cart.
This is the error message I get
NameError at/orders/new
undefined local variable or method 'map' for #<ActionDispatch::Routing::Mapper::0x4d90868>
This line is highlighted
map.current_cart 'cart', :controller => 'carts', :action => 'show', :id => 'current'
Yes it is obsolete now.
Instead of map.resources just use resources.
So you should write
resources :orders
In Rails 4+ it would look something like this:
My::Application.routes.draw do
get '/cart', to: 'carts#show', defaults: { id: 'current' }
end
That may be post if that's how the route is exercised.
The map argument is now gone. My::Application depends on the name of the application in question.
As always, have the routing documentation handy when doing things like this.
If you're working on an older application you will have to follow the older conventions.

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.

Can controller names in RESTful routes be optional?

With a standard map.resource routing mechanics and several nested resources the resultant routes are unnecessarily long. Consider the following route:
site.org/users/pavelshved/blogs/blogging-horror/posts/12345
It's easy to create in routes.rb, and I'm sure it follows some kind of beneficial routing logic. But it's way too long and also seems like it's not intended to be human-readable.
A nice improvement would be to drop controller names, so it looks like:
site.org/pavelshved/blogging-horror/12345
Clear, simple, short. It may become ambiguous, but in my case I'm not going to name any user "users", for instance.
I tried setting :as => '', but it yields routes like this: site.org//pavelshved//blogging-horror//12345 when generating them by standard helpers.
Is there a way to map resources in such a way, that controller names become optional?
You're looking for the :path_prefix option for resources.
map.resources :users do |user|
user.resources :blogs do |blog|
blog.resources :posts, :path_prefix => '/:user_login/:blog_title/:id'
end
end
Will produce restful routes for all blogs of this form: site.org/pavelshved/bogging-horror/posts/1234. You'll need to go to a little extra effort to use the url helpers but nothing a wrapper of your own couldn't quickly fix.
The only way to get rid of the posts part of the url is with named routes, but those require some duplication to make restful. And you'll run into the same problems when trying to use route helpers.
The simplest way to get what you want would be to create a route in addition to your RESTful routes that acts as a shorthand:
map.short_blog ':user_id/:blog_id/:id', :controller => 'posts', :action => 'show'
You'll have to change the URL bits to work with how you're filtering the name of the user and the name of their blog. But then when you want to use the shorter URL you can use all the short_blog_* magic.
Straight out of the default routes.rb:
map.connect 'products/:id', :controller => 'catalog', :action => 'view'
You could write:
map.connect ':user_id/:blog_id/:id', :controller => 'posts', :action => 'show'
But be sure to include that in the very end of the file, or it will try to match every three levels deep url to it.
Try this
map.pavelshved '/pavelshved/', :controller => :users, :action => view or
map.pavelshved '/:id', :controller => :users, :action => show do | blogs|
blogs.bloging '/:id', :controller => :blogs, :action => show do | post|
post.posting '/:id', :controller => :posts, :action => show
end
end
I hope it work :)
Google "rails shallow routes" for information about this.

Giving a nested route an alias in Rails

If I want to provide an alias for a controller, I can use map.resources :rants, :controller => 'blog_posts' yoursite.com/rants points to the blog_posts controller fine.
How do I give an alias to a nested resource, for example yoursite.com/users/5/rants ?
You may want to try:
map.resources :rants, :controller => 'blog_posts'
map.resources :users do |users|
users.resources :rants, :controller => 'blog_posts'
end
This will give you the yoursite.com/users/5/rants/ url that you are looking for and it will generate the handy methods (for example: users_rants_path(#user))
Hope this helps.

Resources