Skip to main content

26 Forum Series Part 3: Nested Attributes and fields_for

Episode 27 · October 9, 2014

Learn how to use accepts_nested_attributes_for and fields_for to create forms that include associated models in them

ActiveRecord Forms


app/controllers/forum_threads_controller.rb

class ForumThreadsController < ApplicationController
  def new
    @forum_thread = ForumThread.new
    @forum_thread.forum_posts.new
  end

  def create
    @forum_thread = current_user.forum_threads.new forum_thread_params
    @forum_thread.forum_posts.first.user_id = current_user.id

    if @forum_thread.save
      redirect_to @forum_thread
    else
      render action: :new
    end
  end

  private

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

app/models/forum_thread.rb

class ForumThread < ActiveRecord::Base
  belongs_to :user
  has_many :forum_posts

  accepts_nested_attributes_for :forum_posts

  validates :subject, presence: true
  validates_associated :forum_posts
end

Resources

Transcripts

Continuing on our forum series in GoRails, we're going to talk about using the nested attributes in your forums so that you can create multiple records at the same time, and they'll automatically be associated. That's what we're going to do today, and the way we're going to do that is we're going to create a forum thread that also has a forum post in the form, and when you submit that, it will create both records, and you'll be able to create the subject for the thread and the body post all at once.

The first thing you need to do, of course, is set up your forum thread in your controller, so you want to create a new one, but you also want to create a forum post for this thread. We'll create both of those in memory and have that one associated with it and this is used so that the fields for in your new action can actually render the forum post's attributes. So let's dive into our forum now.

Inside our form for the forum thread, we can see that we have just the standard form_for, we also have the regular text_field and the submit button, and that’s about it. If we open this in our browser we can see that you get the subject field and the create button, and that’s all there is to it. Now if we want to actually create the fields for the nested attributes, we can do f.fields_for, and we pass in a symbol in this case for the object that we want, so we want fields_for the forum posts, and this is going to be given a block, and the reason why you want to save this variable p is so that it’s different than the parent. The way that this works is that the p variable knows it’s a forum thread, and it knows that the child is going to be of a forum_post type. That is going to create the proper names in our text fields so long as we do something like p.text_field. So if you use p.text_field it will actually use the forum thread and the forum post in the name of the field and submit it across appropriately to Rails. We want to create a

, and inside of here, we want to create a p.text_area.

app/views/forum_threads/_form.html.erb

<%= form_for @forum_thread do |f| %>


<%= f.text_field :subject, placeholder: "Subject", class: "form-control" %>
<%= f.fields_for :forum_posts do |p| %>
    <div class="form-group">
        <%= p.text_area :body, placeholder: "Add a comment", rows: 10, class: "form-control" %>
    </div>
<% end %>

<div class="form-group">
    <%= f.submit %>
</div>

<% end %>
This is actually going to render now because of the forum posts that we created in memory, and it will display the text area’s body for us, so now if we refresh this page, we’ll see the body and the subject, and we’ll be able to submit these once we update the controllers create action to handle this appropriately, so let’s dive into that now.

There are a handful of things that we need to do in our controller and model to actually make that form submit data correctly to Rails. So the first thing of course, you need to create the thread, and we’re going to use the current user’s forum threads association to make that new thread, and the reason we’ll do that is because this will automatically assign the user id on that new thread. And as you would expect with strong params, we want to make a forum_thread_params method, and it’s going to have:

app/controllers/forum_threads_controller.rb

def create
@forum_thread = current_user.forum_threads.new forum_thread_params
end

private
def forum_thread_params
params.require(:forum_thread).permit(:subject, forum_posts_attributes: [:body])
end
We use forum_posts_attributes: [:body] to also permit the nested forum posts, and we do that by pointing to an array of attributes on that object, so we want to accept the body on a forum post when it gets submitted inside of a forum thread. Now this works to assign it, however we actually need to tell the model that it can allow forum posts attributes, so if we go into our forum_thread.rb (model), we can add an accepts_nested_attributes for :forum_posts, you pass in the symbol for the same name as your association, so in this case: forum posts. And the other thing that we want to make sure happens is that validations will run on forum threads when we save it but we also want validations to run on all of the nested forum posts as well, so you can pass in a validates_associated (method) and you give it the same :forum_posts association name. That will make sure that the assignment of attributes works, to create them on nested models, as well as running the validations on the nested models. So if we go back to the forum threads controller, we can do the typical:

def create
#stuff from earlier
if @forum_thread.save
redirect_to @forum_thread
else
render action: :new
end
end
One last thing before we move on from here is that we want to make sure that these forum threads are assigned so we made sure that they’re assigned to the user_id, but we want to also make sure the forum posts underneath it are assigned their user_id as well.

We have that first forum post, and that one is created because of (@forum_thread.forum_posts.new) and we should only have one. And the form will take all of those that we created here and display them. So we only have one, and that means that we can set the user_id = current_user.id, and that should automatically set that. We don’t ever want that to be passed in as a parameter in the field or in the form, because then you could impersonate someone else if you edit the html, so we want to make sure that these two lines hard code that user id and that you could never change that from editing the HTML in the field

@forum_thread.forum_posts.first.user_id = current_user.id
Now if we create a new thread in our application, I’m just going to call it: “GoRails Forum Series” (Subject), “I can’t wait to record a bunch of these screencasts!” (Body). And if we create this forum thread, you can see that the thread subject got populated, the user id got populated so we can pull out the user and then each post is displayed and the user got assigned as well, and that means that we’ve got the forum thread to create and the forum post, and we assign the user id, all in one big swoop, so if we come back to the homepage here and click on new thread, you can actually play with how this works in Rails by just deleting this new post in your new action, and if you refresh the page, the body goes away, so the fields_for is actually looking at these associated models in memory that we created here, and that determines how the form gets rendered, and then all of those get posted, rails parses this and so on. So you can actually go through a whole lot of this and play around with it, and you can make duplicates here, and you can see that there’s two comments now, and fiddle with this, because that way you can create something like a to-do list and a gem like cocoon actually does this very similarly where it keeps track of the html that you generate and then allow you to just duplicate it with JavaScript and then submit like a question and multiple answers all at once in one form. So you can add a whole bunch of dynamic capabilities to this just by playing with some of the functionality that rails naturally works with. So this is really really neat, and I hope you enjoyed this episode.

Next episode we’re going to talk about using the div_for method to highlight and automatically scroll people down to a section of the page, so as you can see here, when we loaded this, it automatically highlighted this post as the new post on this page and then scrolled you down automatically. So we’ll talk about that and how it’s useful next episode, and I will see you next week.

Transcript written by Miguel

Discussion