How to construct the following JSONAPI url in Rails? - ruby-on-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.

Related

Mask URL path on Ruby on Rails

I'm trying to mask the following Route
get '/products/1' => 'products#show', :as => "map"
as hotels/2/map inside my Application, but I'm having no luck. I'm not sure what I'm doing wrong here, as this is already nested inside the Hotels resource. I'm automatically creating two products when the hotel is created, so 1 will be the Map and 2 will be the App.
resources :hotels do
resources :contacts
resources :products
get '/products/1' => 'products#show', :as => "map"
end
What you've got will create hotel_map_path at /hotels/:hotel_id/products/1(.:format). If you instead want map_path without the hotel_prefix, you need to move the map route out of the hotel namespace, like so:
resources :hotels do
resources :contacts
resources :products
end
get 'hotels/:hotel_id/products/1' => 'hotels/products#show', :as => "map"
Hope that helps
edit
I'm gonna revise my suggestion. What you're trying to do will create a dependency between the routes and the details of db storage (the id), which feels wrong. Instead, I'd suggest pushing that dependency into the active_record model, which is the only class that should be responsible for knowing about how Hotels and Products are persisted.
So, your route would be something like:
resources :hotels do
member do
get :map
end
resources :contacts
resources :products
end
which should give you
/hotels/:id/map
in the hotels_controller you'll have
def map
#hotel = Hotel.find(params[:id])
#map = #hotel.location_map
end
and in the Hotel model:
def map
products.location_map
end
and in the Product model:
def location_map
...
end
or add a scope instead.
It's more lines to get the same routing effect, but less brittle.

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)

Rails 3: Different namespace inside nested resource?

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

Is it possible to add more routes when I already did resources :posts

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

Rails 3 - Nested Routes- Help Requested

Here is my Rails 3 nested routes structure for PROJECT
resources :projects do
resources :notes, :photos
collection do
get 'yourproject', 'newjs'
end
end
This works great for things like
/projects
/projects/1
/projects/1/notes/
/projects/1/notes/3
what isn't working is:
/projects/1/notes/newjs
Anyone Rails 3 nested resource experts out there? thanks
You're missing a do in there, so it's defining routes like /projects/newjs
It should be
resources :projects do
resources :notes, :photos do
collection do
get 'yourproject', 'newjs'
end
end
end

Resources