In my Rails 6 app I have a sales_group model which has many sales.
I have a nested form where a user can create a new sales form with x sales in it.
Now I want that the user can add more sales later on so I have:
#sales_group.sales.build
and
<%= form.fields_for :sales do |builder| %>
<%= render "sale_fields", f: builder %>
<% end %>
But this shows nested forms for the new sale and the existing sales. In this add sales form I would like to show only the form for the new sale and not the existing ones.
How could I achieve this?
Related
I'm using the cocoon gem in a Rails4 app to create a nested form
dynamically. Each Lease object my have zero or more MonthlyCharge
objects associated with it. (I.e., the Lease model has_many
:monthly_charges, and the MonthlyCharge model belongs_to :lease.)
I have a form for leases in app/views/leases/_form.html.erb that
calls monthly charges:
<h3>Monthly Charges</h3>
<div id="monthly_charges">
<%= f.fields_for :monthly_charges do |monthly_charge| %>
<%= render 'monthly_charge_fields', :f => monthly_charge %>
<% end %>
<div class="links">
<%= link_to_add_association "Add a Monthly Charge", f, :monthly_charges %>
</div>
</div>
But when I click on "Add a Monthly Charge", three sets of form fields
appear, rather than one.
On top of that, no matter if I fill out the form for one monthly
charge or not, and regardless of whether I click on "Remove Charge"
for the others, rails doesn't save the monthly charge.
Gist is
here.
What can I possibly be doing wrong? It was suggested that I might be
calling the cocoon js file more than once, but I've searched through
the source tree and haven't found any but a single reference to it, in
app/assets/javascripts/application.js. Any input appreciated. Thanks.
For a personal invoicing app in rails I could use some advice on the following: In a new invoice form, how to dynamically add a new row consisting of a collection_select for products and an associated text_field for amount by clicking add new product?
Since one invoice has many products and a product has many invoices, I figured this is a HABTM relationship both ways. Therefore I have created the tables invoices, products, and their join table invoices_products.
I would like the invoices/new page to have a form where a user can use a collection_select to pick a product (all products are already preloaded in the products table) and fill in the amount in a text field behind it.
A user should be able to duplicate these two fields (collection_select and text_field) by clicking add new product, something like RailsCast #403 Dynamic Forms 04:50.
Now how would I go about doing that? And in what table would I put the data from the amount text_field, since there are multiple products per invoice so multiple amounts?
Any answer in the right direction will be greatly appreciated!
This depends on how many additional fields you want
A good tutorial for this can be found here
Ajax
The right way would be to add the element dynamically with Ajax
This is tricky because it means you're not going to have the form_builder object available for the dynamically added items. Regardless, you can still do it with this code:
#config/routes.rb
get "new_field_path", to: "invoices#new_item"
#app/views/controller/index.html.erb
<%= ... form %>
<%= link_to "Add Field", new_field_path, remote: :true %>
Controller
#app/controllers/invoices_controller.rb
def new_item
#invoice = Invoice.new
#invoice.products.build
render "new_item", layout: false
end
#app/views/invoices/new_item.html.erb
<%= form_for #invoice do |f| %>
<%= render partial: "products_fields", locals: { f: f } %>
<% end %>
#app/views/invoices/_products_fields.html.erb
<%= f.fields_for :products, child_index: Time.now.to_i do |p| %>
<%= p.text_field :name %>
<% end %>
Form
#app/views/invoices/new.html.erb
<%= form_for #invoice do |f| %>
<%= render partial: "products_fields", locals: { f: f } %>
<%= link_to "Add Field", new_field_path, remote: :true %>
<%= f.submit %>
<% end %>
Generally if we can create only 1 record, say 1 user at a time from the form. But what if I want to create multiple users from a single form? There are no associations with other models. How can i do that?
You have to make a form with an array of users params .e.g
<%= from_tag '/users/create_multiple' do %>
<%= text_field_tag "users[][name]" %>
<%= text_field_tag "users[][name]" %>
<% end %>
In UsersController:
def create_multiple
params[:users].each do |user|
user = User.create(user)
end
end
You can add validation code as per your wishes,
visit here how to pass form params for multiple records http://guides.rubyonrails.org/form_helpers.html#basic-structures
One option is, write as many forms that you want for each model and submit all the forms once with Jquery.
This link will help you with submitting multiple forms
Multiple submit buttons/forms in Rails
I was stuck with a different scenario like say for example i have model movies and the other is releases. I want to create a new form in the apps/views/movies/new.html.erb. In the new i wanna have the fields for movies table eventually ill be having a form for it and at the same time i want to add the movie release information to the other table releases.Like it will be nested form as follows
<%= form_for(#movies)......%>
<% files for movies table %>
2nd form <%=form_for :releases %>
<% fields for releases table%>
so form one carries the data to movies table when user clicks on submit button. But i want to pass the the values to release table also with this click using a nested form. Is it possible to have nested forms. If so i wanted some help on how it can be achieved exactly. Help me out please.
<%= form_for(#movies) do |f| %>
# fields for movie
<%= f.fields_for :releases do |nested_f| %>
#fields for your nested form releases
<% end %>
<% f.submit %>
<% end %>
Also in Movie.rb model add this line. accepts_nested_attributes_for :releases
More information there
I don’t think HTML allows nested forms, see for example this question.
I want to create a nested object of comments in page form
This returns all comments as checkboxes
<% for comment in Comment.find(:all)> %>
<%= check_box_tag "page[comment_ids][]", comment.id,
#page.comments.include?(comment) %>
<%= comment.navn %>
<br />
<% end %>
When I submit the page form. I want it to take those checked checkboxes and create new comments precise as those there is checked and also a association to page.
**Example:
I get a checkbox list of comments in the page form
Comment 1
Comment 2
I check checkbox 2
When i submit the page form
There is created a new Comment precise as comment2(not the ID), with HABTM association to the page.**
Can someone please tell me how i should create this form?
PS: I am a rails beginner