Rails 3: Different namespace inside nested resource? - ruby-on-rails

Is there a way to use a (different) namespaces inside nested resources? For example,
I have:
resources :users do
resources :tags
end
and I'd like to place the tags controller inside controllers/common, while placing users controller inside controllers/user, with the equivalent for templates.
If I try this:
namespace :user do
resources :users do
namespace :common do
resources :tags
end
end
end
I'll get redundant route names:
user_common_tags, etc. But I want something like common_tags

This way you will have common_tags, and users_tags, both linking to the same controller.
resources :users do
resources :tags
end
namespace :common do
resources :tags
end

Related

How to construct the following JSONAPI url in Rails?

Im following http://jsonapi.org/format/#document-top-level
I notice the following end-point (route):
/articles/1/relationships/author
In Rails, how would this route be contstructed in routes.rb?
resources :articles do
# What goes here?
# Should relationship be a namespace or other?
# I guess author can be defined as a collection, or just a simple get
end
relationships doesn't need to have any of the 7 RESTFUL actions.
The route should be like following code snippets:
resources :articles do
resources :relationships, :only => [] do
collection do
get :author
end
end
end
This is how the route file should look like. Please let me know if any update needed.
Bouncing on my idea of concerns, as you probably will have to use this in several resources (the rails doc is quite well-written)
concern :has_author do
scope '/relationships' do
resource :author
end
end
resources :articles, concerns :has_author
Which is equivalent to
resources :articles do
scope :'/relationships' do
resource :author
end
end
The scope :'/relationships' didn't work for me (At least in Rails 5).
I got it working with this:
resources :articles do
resources :author, path: 'relationships/author'
end
This will just use the AuthorController and map it to /articles/:id/relationships/author.

rails routing: nested resources with different namespace

I am trying to use the acts_as_commentable GEM to add comments to my Post Model. I am using ABC as namespace, so all my controller and models named as ABC::Post, ABC::User, etc.
Currently my routing setup is as following.
namespace :abc do
resources :post do
resources :comments
end
end
The routing URL generated is
POST /abc/post/:id/comments(.:format) abc/comments#create
How can i make it to
POST /abc/post/:id/comments(.:format) /comments#create
founded the answer
namespace :abc do
resources :post do
resources :comments, controller: '/comments'
end
end

Rails generate in namespace one more namespace, there resource and there one more resource: subfolder prblem

I can't understand how can i generate resource in namespaces, and resource in resource: have problems with subfoldering...
How could i generate in rails controller's and views for such route:
namespace :admin do
namespace :catalogs do
namespace :parfume do
resource :brand do
resource :models do
i try so:
rails g scaffold Admin::Catalog
etc...
rails g scaffold Admin::Catalog::To::Brand
but in route i see that rails is generating many lines of route of such kind:
namespace :catalogs do
namespace :to do
namespace :brand do
namespace :models do
namespace :types do
resources :to_articles
end
end
end
end
end
namespace :catalogs do
namespace :to do
namespace :brand do
namespace :models do
resources :types
end
end
end
end
namespace :catalogs do
namespace :to do
namespace :brand do
resources :models
end
end
end
but i want that it must be done via namespaces and resources, not so how now rails generators do...
So which command will be good for generating resource in subfolder one time as namespace, other as resourse?, and am i on right way?
Note: it must be so long path!
Please have a try with
rails g scaffold Admin/Catalog/Parfume/Brand
It will create the routes as following
namespace :admin do
namespace :catalog do
namespace :parfume do
resources :brands
end
end
end
You can do it manually. Lets you have admin and catalog namespace. In side the catalog namespace you have parfume and brands as controllers. And suppose users is a controller in the admin namespace.
So through the following code, you will create the parfume and brands controllers.
rails g controller admin/catalog/parfume
rails g controller admin/catalog/brands
This will create the controllers in your application. But in the routes file you need to add the code manually as following
namespace :admin do
namespace :catalog do
resources :parfume
resources :brand
end
end
But if you have brands as the nested resources of parfume controller, then you need to change the routes little bit as following
namespace :admin do
namespace :catalog do
resources :parfume do
resources :brand
end
end
end
And then, as per our example, we have an users controller in the admin namespace. So we need to create an users controller in the admin namespace, Following is the code to create users controller
rails g controller admin/users
And in addition to it, we need to add a line of code in our routes file as well.
namespace :admin do
namespace :catalog do
resources :parfume do
resources :brand
end
end
resources :users
end
Scaffolding creates the controller and model but as per your requirement, it is creating the extra code in routes. SO we can optimize it as per the above example.
And regarding creating model, you can create it by the following command
rails g model YourModelName

Rails3 nested routes with default base

From the example in rails guides, routes like:
resources :publishers do
resources :magazines do
resources :photos
end
end
Will lead to, URLs like:
/publishers/1/magazines/2/photos/3
I want to have slugs for publishers for example - Oxford
And avoid the first "/publishers" part
Making the URLs to something like:
/oxford/1/magazines/2/photos/3
What is the cleanest and best way to achieve this in Rails 3?
scope :path => ":publisher_slug", :as => "publisher" do
resources :magazines do
resources :photos
end
end
publisher_magazine_photo_path("oxford",2,3)

How can i override a scope route with a named route in rails 3?

I have this scope:
scope ":section", :section => /[a-zA-Z_]+/ do
resources :case_studies, :promotions, :events
end
URL example: :section/case_studies
And i have a named scope:
namespace :admin do
resources :case_studies, :promotions, :events
end
URL example: admin/case_studies
The problem is the admin case studies, promotions, and events are registering the "admin" namespace portion as a section variable. Is there a way to limit the scope more or have admin take precedence over it?
Routes are executed top-down in your routes.rb file. If you make sure they're in this order:
namespace :admin do
resources :case_studies, :promotions, :events
end
scope ":section", :section => /[a-zA-Z_]+/ do
resources :case_studies, :promotions, :events
end
Then the admin routes should take precedent over your section routes.
for precedence move the admin namespace higher up in your routes.rb file

Resources