Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jhjguxin/3074080 to your computer and use it in GitHub Desktop.
Save jhjguxin/3074080 to your computer and use it in GitHub Desktop.
Creating nested resources in ruby on rails 3 and updating scaffolding links and redirection
@jgjerset
Copy link

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

@dmakhmutov
Copy link

good tutorial. thanks

@valdemarua
Copy link

Good tutorial. Thank you.

@oskarlakner
Copy link

Very helpful, thank you!

@nguyenj
Copy link

nguyenj commented Jan 3, 2014

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

@nslindtner
Copy link

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
Copy link

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

@ThomasBush
Copy link

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
Copy link

awesome article.

@codemonkey3045
Copy link

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

@wangguoqin1001
Copy link

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

@Tasha25
Copy link

Tasha25 commented May 27, 2014

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

@christianrojas
Copy link

Awesome! thanks

@trooney
Copy link

trooney commented Aug 5, 2014

Awesome!

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

@chaz1505
Copy link

great tutorial thanks

@ricardocastaneda
Copy link

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
Copy link

lortza commented Nov 18, 2015

Beautiful work. Thank you!

@khatchad
Copy link

khatchad commented Mar 9, 2016

@nguyenj Any luck with rails 4?

@khatchad
Copy link

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

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

@DivyaChouhan
Copy link

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

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