How can I add dynamic fields to a rails form to create multiple objects from the same model? I'm able to do this with nested forms using the cocoon gem but for fields of the same model I can't figure it out. I tried to add fields dynamically trough js but without success! Can anyone point me on the right direction? Thanks.
Related
I am working with a Rails 6.1 project and am using the twitter-bootstrap-rails gem to create a form using bootstrap_form_for for an object that has an association.
I would like to add the association fields to the form, and am finding that Rails has a fields_for, helper but I am not finding any equivalent in twitter-bootstrap-rails.
I was successfully using the fields_forhelper until I needed to create a form_group in it for radio buttons, where the data for the group is coming from an array of hashes.
Although the fields_forhelper does have the ability to create radio button groups, I am not seeing how to use a hash to populate the radio button fields.
How does one add form fields for an object's association, when using the twitter-bootstrap-rails gem maintaining the the full form_group capabilities?
If this isn't possible, can anyone point me to documentation on creating a radio button group under fields_for using an array of hashes as the data source?
Many thanks in advance for your suggestions.
I have a PurchaseOrder Model that has_many Items. The form for PurchaseOrder needs variable input fields that will also save Items, where clicking the Add button will increase the fields in the page.
Here's what it will look like:
In order to achieve this:
How do I create a simple_form that will post the result of these multiple fields as an array to my existing PurchaseOrdercontroller where I can process and add these records?
Bonus: how would I handle this via Cucumber?
Edit: Why would you downvote a self contained question? Leave comments to explain when you downvote, please.
Use Cocoon as your nested forms for creating multiple fields.
You need to use simple_fields_for from simple_form gem. This allows you to work with attributes of associated models.
If you want to add new associated models via Add new button you need to create a new row. There is no code in pure simple_form that will help you with that. I found gem cocoon. It looks like it's what you're looking for. You can take a look how it's implemented there and make your own light solution.
I have two models in my project: Invoice and InvoicePosition. Invoice has many InvoicePositions. I want to create a form for new invoice but with additional fields to create also invoice positions in same time. I want to make in dynamically, i mean i want to add more invoice positions by adding new fields with javascript. What is the best way to achieve this?
This can be solved using nested model forms, e.g. accept_nested_attributes_for :invoice_position. You can find more information about it here. The dynamic part is achieved through javascript.
Use simple_form_for gem it has options for associations
My issue is simple, lets say I have two models/tables named 'abc' and 'pqr', both has three columns as 'a','b','c' in abc and 'p','q','r' in pqr. This two models may or may not be related/nested.
what I want to do is to create a single webpage. On that webpage I want to create a single form which will submit the data for two models/table with single button. May be I will create two form but I want only one submit button. How do solve this issue in ruby on rails.
As in rails we have one model per table.
You can only use accepts_nested_attributes_for if the two models are related. Otherwise, if the models are unrelated, see Anton's answer in rails: a single simple_form with two unrelated models? describing how to use the fields_for helper to accomplish this.
I can suggest you something in Ruby side. It is possible to do that with accepts_nested_attributes_for method.
You can add to
models/abc.rb
accepts_nested_attributes_for :pqr
You can find more information about it here.
http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html
I have a simple rails application with a table created using the rails generate scaffold command. It allows the user to add the names of different movies using a form i.e. automatically generated by rails. I want to add an option where a User can add the name of a director like Christopher Nolan and then add the name of all the movies of that director within it, sort of like a group in the rows. Also all the subrows need to be linked to each other. Is this possible on rails?
P.S. I know that Stack Overflow works on the concept where I add a snippet of code for help, but since I'm a newbie I don't have any idea about how to even start with this
About nested model form http://railscasts.com/episodes/196-nested-model-form-part-1
And this gem can help u for create form with list items
https://github.com/nathanvda/cocoon