Forum Series Part 3: Nested Attributes and fields_for Discussion
What do you think about using ActiveForm? https://github.com/rails/ac...
Also there is a gem `reform` for helping with this is https://github.com/apotonic...
What markdown parser gem did you use for this episode Chris?
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'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 have tried this but seem to have a problem.
When I do:
@post = Post.new
I get a undefined method 'build' for nil:class
but it will work if I do this:
@post = Post.new
has_one: :content, dependent: :destroy
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 @post.contents.build and @post.contents.create
Much more information here: http://api.rubyonrails.org/...
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:
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
Is there an easier way to do this? what i basically want is kind of a thread within a thread
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: https://github.com/platafor...
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.
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'