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
Related
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.
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
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)
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
In my routes i have:
resources :posts
now say I want to create a new action/view, is it possible to add it inside a block like:
resources :posts do
// new routes other than the show/new/create/delete/update that REST gives me.
end
Yep
resources :post do
get :action, :on => :member
get 'other', :on => :collection
post :change, :on => :member
resources :another_model
end
note: these are just samples of what you can do and this does assume Rails 3. For more information you should read the Ruby on Rails Guide: Rails Routing from the Outside In
Yes, just add them in. For example, if you want to be able to post to "new_action" you would do
resources :posts do
post "new_action"
end
Have you looked at the Rails Guide: Rails Routing from the Outside In? Section 2.9 Adding More RESTful Actions describes exactly what you're looking for. I frequently refer to the other guides there too.
You can do this for a resource member
resources :posts do
get :preview, :on => member
end
or this for a resource collection
resources :posts do
get :active, :on => collection
end
When I was starting out with rails I was confused about what members and collections were. For a Post resource a member is an individual post while a collection is the collection of all posts. For example...
# url for a member
/posts/5/preview
# url for a collection
/posts/active
The rails guide goes into more detail on routing