All threads / Forum Series Part 3: Nested Attributes and fields_for Discussion

Ask A Question


You’re not receiving notifications from this thread.

Forum Series Part 3: Nested Attributes and fields_for Discussion

Daniel Kehoe ·

What do you think about using ActiveForm?

This looks awesome. I'd love to see this stuff standardized and included in Rails core. Is the plan for this shipping with Rails 5?

Daniel Kehoe ·

I hope so!


Also there is a gem `reform` for helping with this is


What markdown parser gem did you use for this episode Chris?

I'm using pygments.rb inside of Github's html-pipeline with a handful of other gems to improve the Markdown parsing:

gem "html-pipeline", "~> 1.9.0"
gem "rinku", "~> 1.7.3"
gem 'github-linguist', '~> 3.1.2'
gem "github-markdown", "~> 0.6.4"
gem 'pygments.rb', '~> 0.6.0'

You flew by the form pretty fast. Can you include that code on this page as well. I don't have the view files, so create them manually? Thanks!

I just updated the notes to include the link to the Github project but it's here too!


I'm very new at this, so I have a few questions.

How come my forum_threads_controller was blank, but the video started with some code already?

Also, my views folder has none of the same files that the video has at this point? Are will we be creating these as we go?

I skipped a few of those pieces to focus on the topic in this episode. The controller and views are basically just those you get from generating a scaffold.

In the future, I'll be sure to include the code for you in between.


I have tried this but seem to have a problem.

When I do:

def new
@post =

I get a undefined method 'build' for nil:class

but it will work if I do this:

def new
@post =

My models:

has_one: :content, dependent: :destroy
accepts_nested_attributes_for :content

belongs_to :post

Any ideas why?

has_one and has_many are different and you'll interact with them differently.

has_one :content makes methods like @post.build_content and @post.create_content

has_many :contents makes methods like and @post.contents.create

Much more information here:

thanks for the reply Chris Oliver!

Most of the examples I saw were using has_many relationships so I guessed I missed it.

There are examples tho that I've seen in strong params:

def forum_thread_params
params.require(:forum_thread).permit(:subject, forum_posts_attributes: [:id, :body])

is including the :id necessary?

I can't quite remember. You can try without and see if things like update work correctly and don't create a new forum_post. It might be required so it knows which record to update, but it might not. You may need to play with that to verify.

I just saw this in a comment for the top answer (link at the bottom):

"DON'T FORGET THE ID!!!! pets_attributes: [:id, :name, :category] Otherwise, when you edit, each pet will get created again"

Perhaps this is why. Didn't manage to test it as I'm not enabling edits for my app.


Hi Chris, How can I make it create x amount of posts, specified by the thread starter, each with a different subject, again specified by the thread starter. For example:
New thread Name= Cars
Post= Mercedes
Post= BMW
Post= Porsche
Is there an easier way to do this? what i basically want is kind of a thread within a thread

OMG I just rewatched this sorry this is awesome! But is there a way for the user to determine how many comments to add?


Hi Chris! I'd like to create a form for a product model, where users can choose a product category first and then can fill the form out. This would be easy, but I'd like to show them different attributes based on the chosen category. Something like if they choose book category, then they will have fields like title, author, published_at, but if they choose shoes category then they can fill out the size, color and type fields. What is the good approach in this case? Should I create more different models like (shoes,books, etc.) or something else? I saw some tuts about dynamic form, but as far as I understand it, I don't need that since the form fields will be predefined and users won't be able to add extra fields.


Hi Chris, great videos! What are your thoughts about using simple_form gem instead of the built in form_for helper? Are there any advantages/disadvantages using this gem?

Simple from gem:

Pros are you get a lot of helpers for making basic forms quicker, downside is that I often customize my forms a lot so you can't really use their helpers all the time and it's also another API to continuously memorize. I use them in things like Admin areas or forms that don't need much UI work, but other than that, I tend to just use the normal form helpers and tags.

Thank you for the quick comparison Chris. Keep up the great work!


This episode was hard to follow because there was a lot added that wasn't covered on video.


If someone is using Rails 5, notice that "the relational model MUST need the foreign key to create the instance", because the forum_thread has not created yet, the foreign key in forum_post is missing.

In Rails 5, need to add ', optional: true' to forum_post.rb

'belongs_to :forum_thread' >>> 'belongs_to :forum_thread, optional: true'

Douglas Lawrence ·

My afternoon has been spent without this information and it was a confusing afternoon. This is exactly what I needed to know. THANK YOU :)

omg. it is worst course i have ever seen. can anybody tell me where did he show that he added bootstrap?
I didn't cover it. Early screencasting mistake. You can follow this episode to install Bootstrap.
Join the discussion


Join 45,185+ developers who get early access to new tutorials, screencasts, articles, and more.


    logo Created with Sketch.


    © 2021 GoRails, LLC. All rights reserved.