Create form for recursive model - ruby-on-rails

Hello I am trying to create a form in which Model A has many and belong to itself. I tried using nested_form and no luck yet.
I want to create nested form for just Model A.
Can anyone suggest me correct path ?

Without more details on your models and structure it's hard to help, maybe include some code too?
For nested models you typically do something like this:
<%= form_for #model_a do |model_a_form| %>
<%= model_a_form.text_field :attribute_1 %>
<%= model_a_form.text_field :attribute_2 %>
<%= fields_for :child_models, #model_a.child_models do |child_models_fields| %>
<%= child_models_fields.text_field :attribute_1 %>
<%= child_models_fields.text_field :attribute_2 %>
<% end %>
<%= model_a_form.submit %>
<% end %>
See https://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for for more info.

Related

Using a loop to create 10 similar fields in Rails

I'm a beginner on my first project. If I have 10 similar fields, is there a way to create them dry so they would still work with validations? I would also like to put the loop number in the attribute name, because the attributes are named field_name_1 through to field_name_10, for example.
<% 10.times do |asdf| %>
<%= f.input :field_name_(asdf + 1), etc %>
<% end %>
This may work !!!
<% (1..10).each do |n| %>
<%= f.input ("field_name_"+n.to_s).to_sym, etc %>
<% end %>

Rails habtm checkboxes on create

Is possible to utilize the habtm checkboxes on create action?
because this:
<%= hidden_field_tag "product[size_ids][]", nil %>
<% Size.order(:size).each do |size| %>
<li> <%= check_box_tag "product[size_ids][]", size.id, Product.size_ids.include?(size.id), id: dom_id(size) %>
<%= label_tag dom_id(size), size.size %>
</li>
<% end %>
was on update and was working since was brought to create page rails spits out the
undefined method `size_ids' for #
so, have a way to utilize the habtm on a create action?
Since you're likely dealing with one item, you probably mean:
#product.sizes_ids
The Product model doesn't have a direct association with any sizes, it's only instances of it that do.
use collection check boxes getting all of the model with ids exemple:
<%=p.collection_check_boxes :size_ids, Size.all, :id, :size %>

multiple forms for different instances of the same object class on the same page in Rails

I have a page with many posts, and each post has a list of comments. At the end of the list is a form for a user to add a comment. Only one comment can be submitted at a time.
Can I get away with something like:
form for #comment
...
form for #comment
or do I need to specifically make sure each form is for a separate object? ie
form for #comment1
...
for for #comment2
If it's the latter, how can I make the main page's controller create one comment object for every post on the page?
You need something like this on your view
<% #posts.each do |post| %>
...
<%= form_for post.comments.build do |f| %>
<%= f.hidden_field :post_id %>
...
<% end %>
<% end %>
or, if you use nested resources in you routes
<% #posts.each do |post| %>
...
<%= form_for [post, Comment.new] do |f| %>
...
<% end %>
<% end %>
You can use Nested model form for this purpose.

form_for : how to bring 2 variables into view

How can i bring 2 variables into the view. I am newbie in Ruby on rails.
What will the sytax to bring 2 or more values into a view.
<%= form_for(#user) do |f| %>
EDIT :
<%= form_for(#user) do |f| %>
prohibited this user from being saved: </h2>
<u1>
<% #user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</u1>
</div>
<% end %>
<%= debug #user %>
<div class = "field">
<%= f.label :email %><br/>
<%= f.text_field :email %>
</div>
Let's say that in the above code I want to print values from 2 objects and also submit them. How can I do that ?
Well, if you need to use 2 variables, I think it would be better to use two separate forms because they are unrelated. If two variables have relations to each other, you should use accepts_nested_attributes_for and fields_for to do work.
http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for
from your above comment "I am talking about loading 2 model into one form" I think you need to use two model in one form so rails cast produce good episode on using nested model for form .
You can use accepts_nested_attributes_for for eg. for survey is one model and question is another model you can use question as nested_attributes in survey and same you can use answers model in survey model in same form.
For more you can read following link.
http://railscasts.com/episodes/196-nested-model-form-part-1?view=asciicast
I hope this will help you.
Thanks.

Formtastic. How to create a form for multiple objects of the same model?

I have a subject model with two fields - "name" and "level". I want to be able to visit "/subjects#new" and add 10 subjects objects at once. How can I do this in formtastic.
In regular forms I would do this:
<% #subjects.each do |s| %>
<% fields_for "subject[#{s.id}]", s do |f|%>
<%= f.name%>
<%= f.level %>
<% end %>
<% end %>
Change fields_for to semantic_fields_for for and it should work.

Resources