Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save jhjguxin/3074080 to your computer and use it in GitHub Desktop.

Select an option

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

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

@dmakhmutov
Copy link
Copy Markdown

good tutorial. thanks

@valdemarua
Copy link
Copy Markdown

Good tutorial. Thank you.

@oskarlakner
Copy link
Copy Markdown

Very helpful, thank you!

@nguyenj
Copy link
Copy Markdown

nguyenj commented Jan 3, 2014

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

@nslindtner
Copy link
Copy Markdown

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

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

@ThomasBush
Copy link
Copy Markdown

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

awesome article.

@codemonkey3045
Copy link
Copy Markdown

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

@wangguoqin1001
Copy link
Copy Markdown

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

@Tasha25
Copy link
Copy Markdown

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

Awesome! thanks

@trooney
Copy link
Copy Markdown

trooney commented Aug 5, 2014

Awesome!

@gtheys
Copy link
Copy Markdown

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

great tutorial thanks

@ricardocastaneda
Copy link
Copy Markdown

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

lortza commented Nov 18, 2015

Beautiful work. Thank you!

@khatchad
Copy link
Copy Markdown

khatchad commented Mar 9, 2016

@nguyenj Any luck with rails 4?

@khatchad
Copy link
Copy Markdown

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

@EricLondon
Copy link
Copy Markdown

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

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

@mices
Copy link
Copy Markdown

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