Ask A Question

Notifications

You’re not receiving notifications from this thread.

Forum Series Part 3: Nested Attributes and fields_for Discussion

Daniel Kehoe Daniel Kehoe

What do you think about using ActiveForm? https://github.com/rails/ac...

Reply

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?

Reply

Also there is a gem `reform` for helping with this is https://github.com/apotonic...

Reply

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

Reply

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'

Reply

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!

Reply

I just updated the notes to include the link to the Github project but it's here too! https://github.com/excid3/g...

Reply

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?

Reply

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.

Reply

I have tried this but seem to have a problem.

When I do:

def new
@post = Post.new
@post.content.build
end

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

but it will work if I do this:

def new
@post = Post.new
@post.build_content
end

My models:

Post.rb
has_one: :content, dependent: :destroy
accepts_nested_attributes_for :content

Content.rb
belongs_to :post

Any ideas why?

Reply

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 @post.contents.build and @post.contents.create

Much more information here: http://api.rubyonrails.org/...

Reply

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

Reply

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

Reply

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.

Reply

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: https://github.com/platafor...

Reply

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.

Reply

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

Reply

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'

Reply
Douglas Lawrence 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 :)

Reply
omg. it is worst course i have ever seen. can anybody tell me where did he show that he added bootstrap?
Reply
I didn't cover it. Early screencasting mistake. You can follow this episode to install Bootstrap. https://gorails.com/episodes/bootstrap-4-rubygem
Reply

Thank you :)

Reply

forum_threads_controller
class ForumThreadsController <ApplicationController

before_action :authenticate_user!, except: [:index,:show]
before_action :set_forum_thread,except: [:index,:new,:create]

def index
@forum_threads = ForumThread.all

end

def new
@forum_thread = ForumThread.new
@forum_thread.forum_posts.new
end

def create
@forum_thread = current_user.forum_threads.new forum_thread_params
if @forum_thread.save
redirect_to @forum_thread
else
render action: :new
end

end

def show

end

private
def set_forum_thread
@forum_thread = ForumThread.find(params[:id])
end

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

forum_posts_controller
class ForumPostController < ApplicationController

def index
@forum_posts = @ForumPost.all
end

def new
@forum_post = @ForumPost.new
end

def edit

end

def show

end

def create

end
end

form partial

<%= form_with model: @forum_thread do |form| %>

<%= form.text_field :subject, palceholder: "subject", class: "form-control" %>


<%= form.fields_for :forum_posts do |p| %>


<%= p.text_area :body ,palceholder: " Add a coment", rows: 5, class: " form-control" %>

<% end %>

<%= form.submit %>

<% end %>

forum_thread> new.html.erb
<%= render 'forum_threads/form' %>

Shows
unknown attribute 'forum_thread_id' for ForumPost.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

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

    We care about the protection of your data. Read our Privacy Policy.