Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Creating nested resources in ruby on rails 3 and updating scaffolding links and redirection
Copy link

jgjerset commented Nov 19, 2013

Rails noob and had been trying to figure this out for hours. Your tutorial helped me tremendously. Thanks.

Copy link

dmakhmutov commented Nov 21, 2013

good tutorial. thanks

Copy link

valdemarua commented Nov 22, 2013

Good tutorial. Thank you.

Copy link

oskarlakner commented Dec 19, 2013

Very helpful, thank you!

Copy link

nguyenj commented Jan 3, 2014

This is great, can you also provide an update to Rails 4?

Copy link

nslindtner commented Jan 11, 2014

Nice gist. Perhaps you want to add the change to config/routes.rb files, made after adding the relation between models:

resources :parents do
resources :children

Copy link

sangwonl commented Jan 22, 2014

Great tutorial, indeed! I've struggled how to start from scaffolding.. (It's confusing to me..) Good stuff to learn!

Copy link

ThomasBush commented Jan 24, 2014

Hey I recently had a polymorphic association where I used this. In my case blocks(child) - products(parent) and blocks(child) - technologies(parent) were the associations. Products and technologies (and any number of other models) are parents and each have blocks as a child. Your view templates can't hard code just one model as shown in the example:

<%= link_to 'Edit', edit_parent_child_path(@parent, @child) %>

Instead I change controller code that populates the @parent variable so it can be dynamic depending on the parent model. Than in the controller index and show actions to can use:

resource, id = request.path.split('/')[1, 2]
parent_model = resource.singularize.classify.constantize
@parent = parent_model.find(id)

In routes.rb this would look like:

resources :products do
    resources :blocks

resources :technologies do
     resources :blocks

Thought maybe this could help someone else so I wanted to share.

Copy link

helixtesting commented Jan 30, 2014

awesome article.

Copy link

codemonkey3045 commented Feb 26, 2014

This is amazing! I've been stuck on this all day. THANK YOU! THANK YOU! THANK YOU! =D

Copy link

wangguoqin1001 commented Apr 1, 2014

Why hasn't there been a way to "rails g scaffold" a nested resource directly (or has there been one already)?

Copy link

Tasha25 commented May 27, 2014

How did you create this gist? I want to create steps to the various apps I build.

Copy link

christianrojas commented Jun 4, 2014

Awesome! thanks

Copy link

trooney commented Aug 5, 2014


Copy link

gtheys commented Oct 22, 2014

$ rails generate scaffold account name:string
$ rails g scaffold campaign name:string account:belongs_to

Can be used to create the relations

Copy link

chaz1505 commented Mar 24, 2015

great tutorial thanks

Copy link

ricardocastaneda commented Jun 23, 2015

I set myself into the large list of grateful people for this very well explained tutorial. Thanks also to those people from comments who shared code.

Copy link

lortza commented Nov 18, 2015

Beautiful work. Thank you!

Copy link

khatchad commented Mar 9, 2016

@nguyenj Any luck with rails 4?

Copy link

khatchad commented Mar 18, 2016

@nslindtner This fork has the part about the routes.rb changes.

Copy link

EricLondon commented Jun 6, 2016

name: Eric, parent: Paul, sounds a lot like my family. Thanks for the credit.

Here's the original blog post :)

Internet pro tip: when you copy someone's post, you should cite the source.

Copy link

DivyaChouhan commented Jul 15, 2016

Very well explained tutorial. It helps a lot. Thanx...

Copy link

mices commented Apr 3, 2017

You don't say what to put for else if validations cause return to form instead of save

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment