Skip to content

Instantly share code, notes, and snippets.

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

This comment has been minimized.

Copy link

@jgjerset jgjerset commented Nov 19, 2013

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

@dmakhmutov

This comment has been minimized.

Copy link

@dmakhmutov dmakhmutov commented Nov 21, 2013

good tutorial. thanks

@valdemarua

This comment has been minimized.

Copy link

@valdemarua valdemarua commented Nov 22, 2013

Good tutorial. Thank you.

@oskarlakner

This comment has been minimized.

Copy link

@oskarlakner oskarlakner commented Dec 19, 2013

Very helpful, thank you!

@nguyenj

This comment has been minimized.

Copy link

@nguyenj nguyenj commented Jan 3, 2014

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

@nslindtner

This comment has been minimized.

Copy link

@nslindtner 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
end

@sangwonl

This comment has been minimized.

Copy link

@sangwonl 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!

@ThomasBush

This comment has been minimized.

Copy link

@ThomasBush 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
end

resources :technologies do
     resources :blocks
end

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

@helixtesting

This comment has been minimized.

Copy link

@helixtesting helixtesting commented Jan 30, 2014

awesome article.

@codemonkey3045

This comment has been minimized.

Copy link

@codemonkey3045 codemonkey3045 commented Feb 26, 2014

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

@wangguoqin1001

This comment has been minimized.

Copy link

@wangguoqin1001 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)?

@Tasha25

This comment has been minimized.

Copy link

@Tasha25 Tasha25 commented May 27, 2014

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

@christianrojas

This comment has been minimized.

Copy link

@christianrojas christianrojas commented Jun 4, 2014

Awesome! thanks

@trooney

This comment has been minimized.

Copy link

@trooney trooney commented Aug 5, 2014

Awesome!

@gtheys

This comment has been minimized.

Copy link

@gtheys 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

@chaz1505

This comment has been minimized.

Copy link

@chaz1505 chaz1505 commented Mar 24, 2015

great tutorial thanks

@ricardocastaneda

This comment has been minimized.

Copy link

@ricardocastaneda 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.

@lortza

This comment has been minimized.

Copy link

@lortza lortza commented Nov 18, 2015

Beautiful work. Thank you!

@khatchad

This comment has been minimized.

Copy link

@khatchad khatchad commented Mar 9, 2016

@nguyenj Any luck with rails 4?

@khatchad

This comment has been minimized.

Copy link

@khatchad khatchad commented Mar 18, 2016

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

@EricLondon

This comment has been minimized.

Copy link

@EricLondon 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.

@DivyaChouhan

This comment has been minimized.

Copy link

@DivyaChouhan DivyaChouhan commented Jul 15, 2016

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

@mices

This comment has been minimized.

Copy link

@mices 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