Questions tagged [nested-resources]

In REST, nested resource is a web resource composing another. Its reference is built over the reference of another web resource.

Definition

In REST-style architecture , a resource is simply a source of information, the one you want to expose. The resource is referenced by a global identifier such as an URI. When you deal with information structure such as composition, you’ll use nested resources. The reference of the embedded resource is then built over the reference of the composite resource.

For instance, a blog post http://example.com/posts/1 owns several comments http://example.com/posts/1/comments.

372 questions
184
votes
2 answers

REST Complex/Composite/Nested Resources

I'm trying to wrap my head around the best way to address concepts in a REST based API. Flat resources that don't contain other resources are no problem. Where I'm running into trouble are the complex resources. For instance, I have a resource for a…
jgerman
  • 2,101
  • 3
  • 13
  • 9
126
votes
3 answers

form_for with nested resources

I have a two-part question about form_for and nested resources. Let's say I'm writing a blog engine and I want to relate a comment to an article. I've defined a nested resource as follows: map.resources :articles do |articles| articles.resources…
Dave Sims
  • 4,958
  • 3
  • 22
  • 26
40
votes
1 answer

change the URL without changing the resource name

I'm building a website for a rabbit farmer (let's pretend). This man keeps a close eye on his rabbits, and wants them all categorized. So I built him a RabbitCategoriesController, and added this line to my routes.rb resources…
declan
  • 5,404
  • 2
  • 37
  • 42
29
votes
4 answers

Rails - link_to, routes and nested resources

As my understanding on nested resources, on edge Rails, should not link_to 'User posts', @user.posts point to /users/:id/posts ? The routes.rb file contains map.resources :users, :has_many => :posts If this is not the default behavior, can it be…
knoopx
  • 15,557
  • 7
  • 34
  • 41
21
votes
4 answers

RESTful API routes design: nested vs. non-nested

My question is about the advantages of nesting resources when building URLs for API purposes. Consider the following two alternatives for accessing an employee resource: /api/employees?department=1 # flat Vs. /api/departments/1/employees #…
Ernesto
  • 3,307
  • 3
  • 31
  • 49
20
votes
3 answers

Nested resources in Django REST Framework

I wish to implement my new API with a nested resource. Example: /api/users/:user_id/posts/ Will evaluate to all of the posts for a specific user. I haven't seen an working example for this use case, maybe this isn't the right way for implementing…
Tal Peretz
  • 445
  • 1
  • 4
  • 14
18
votes
2 answers

Rails Namespace vs. Nested Resource

Let's say my app has two models, Foo and Bar. Foo optionally belongs_to Bar. Right now I can look at a single Foo, or search for a particular Foo, and the FoosController handles all that. My URLS are like: foos/1 and foos/new Sometimes I want to…
Andrew
  • 40,445
  • 46
  • 172
  • 276
17
votes
3 answers

Rails Nested Singular Resource Routing

I have a simple User model with a singular nested Profile resource so in my routes.rb I have: resources :users do resource :profile, :only => [:edit, :update, :show] end This generates the expected routes: edit_user_profile GET …
15
votes
1 answer

Using nested resources without the parent ID in Rails

I have a class called Imprintables with nested resources Styles, Brands, Colors, and Sizes. I currently have this in my routes file: resources :imprintables do resources :styles, :brands, :colors resources :sizes do collection do post…
davidicus
  • 551
  • 1
  • 5
  • 16
15
votes
3 answers

Rails: Getting the Parent Object of a Nested Resource

I have a nested resource that belongs to many different models. For instance: resources :users do resources :histories, only: [:show] end resources :publications do resources :histories, only: [:show] end resources :events do resources…
nullnullnull
  • 7,579
  • 12
  • 48
  • 101
13
votes
2 answers

RSpec, stubbing nested resource methods

I've got two models: class Solution < ActiveRecord::Base belongs_to :owner, :class_name => "User", :foreign_key => :user_id end class User < ActiveRecord::Base has_many :solutions end and I nest solutions within users like…
TheDelChop
  • 7,478
  • 3
  • 43
  • 67
12
votes
4 answers

Rails: Nested resources conflict, how to scope the index action depending on the called route

Imagine you have two defined routes: map.resources articles map.resources categories, :has_many => :articles both accessible by helpers/paths articles_path # /articles category_articles_path(1) # /category/1/articles if you visit /articles, index…
knoopx
  • 15,557
  • 7
  • 34
  • 41
11
votes
4 answers

How to use Link_to with nested resources

I am a totally newbie in Rails. I have created a web application, I can access through /posts/123/comments/ or /posts/123/comments/new, but i don't know how to use link_to in the index view to show a concrete comment, when i try to link it, appears…
fuco
  • 223
  • 3
  • 11
11
votes
1 answer

Nested resource and restricting the Routes Created :only and :except

Good day all, Can someone kindly assist me with nested resources and its best practice. I would like to restrict my :events route to only :show and :index, is this the correct way of doing it? resources :events do resources :registrations,…
9
votes
2 answers

Getting Rails Nested Resources to Route to correct nested Controllers

In Rails 3.2.11, I have the following route definitions resources :foos do resources :bars resources :bangs, :controller => 'foos/bangs' end which result in the following routes foo_bars GET /foos/:foo_id/bars(.:format) …
1
2 3
24 25