Create objects in form with the same value - ruby-on-rails

I need to build a form that creates 3 records but there is a repeated field in all of these records (the users name).
#competition = Competition.find(params[:competition_id])
#entries = Array.new(3) {#competition.entries.build()}
The entry has the following fields:
Name - String
Description - Text
With this current method the user is required to enter their Name in to 3 different boxes for the three different record entries.
What is the rails approach in stopping the user from having to enter in the same data 3 times?

You need not ask the user for their name three times.
Look into using the nested form_for helper. Something along the lines of
= form_for [#user, #competition] do |f|
…assuming this is what you want to do. Also, when the form is submitted, you can fill in the duplicate fields before saving the object.
Disclaimer: It's been a while since I did Rails. No Rails 4 experience.

Redesign the model so that the repeated attributes are in a has many association with the ones that are always changing. This will give greater flexibility and control in the future.

Related

What's best practice for assigning unique ids to Rails form inputs?

I have a Rails form for multi-editing.
The purpose of this form is to be able to edit attributes for multiple invite objects.
This form looks like:
= form_with url: some_path
- invites.each do |invite|
= fields_for 'invites[]', invite do |invite_form|
= invite_form.email :email
= invite_form.select :role
Due to the fact that all of these inputs are nested under a single form, the ids generated for these inputs are not unique which is problematic for accessibility.
It seems like if I had multiple forms, I could make use of setting namespace, but what's standard for setting the id for something like this where there are duplicate input types under one form (but for different objects)? Is it bad practice to generate some random ids for setting the id? Or just using each_with_index and appending an index? To note, invites is not an active record model so there's no id I can make use of there.

How can I update multiple rows in a Model using a form in Rails?

I'm trying to make a small app in rails where investors can see their balances. The balances are updated by an admin.
The investors are all in a Model called User - is there any way to set up one form in one view where User.balance can be updated for all Users?
The fields in the view can be populated using a <% User.each do |u| %> loop, I guess, but how can I handle the data in the controller?
Thanks!
It might be convenient to implement this in an index view, by using an Ajax-capable gem such as best_in_place, which would let the values be edited in place without the form being submitted.

How do I create a fields_for a potentially unlimited number of children objects?

I'm making an application that involves booking appointments for users. I have a User model and an AvailableDate model. The user has_many availble_dates and the AvailableDate belongs_to user.
I want to present a form for the user so that they can mark a couple of dates in a calendar and each of the dates they mark will become an AvailableDate object tied to that user.
At the moment my solution is to do all the work that a form_for helper would normally do manually. This involves a lot of javascript and is generally just getting far too messy.
I can't figure out how I should make a form_for tag work when I need to create potentially infinitely many dates. In theory a user could keep marking off dates in the future as available. If I knew how many dates I needed to create for a user, I could do user.available_dates.build, N times. But this doesn't work here.
Can anyone help? It like this problem should be pretty common. Am I designing my application wrong?
One technique is to render the fields for your association once, outside the form.
When the user performs whatever interaction that should create a new set of inputs you use javascript to clone the initial set of fields and insert them into the form. The one thing you need to do is change the name of these inputs so that they are unique. Usually people use the current time in milliseconds for this unique identifier.
Been there & have found several resources to help: Tutorial & Cocoon
The bottom line is you need to ensure child_index is unique for each field. The tutorial I use has child_index: Time.now.to_i to create a truly unique id, consequently allowing you to add as many fields as you want
The best way to do this:
Render fields_for as a partial (passing your form builder object)
When you want to add new field, create ajax_field action
Make ajax_field view have its own form_builder
Both your original & ajax_field forms will call the partial
On front-end, you can use JS to GET new form action & append field to page
I can give you code if you want

Rails 3.1 - Create Multiple records to then edit

I have a situation where I want a user to specify the number of records they want to create, have the app then create these blank entries in database and then be presented with an 'edit' view to populate them with their data. I know this is probably bad practice but can anyone give some guidance on how to approach this?
Why do you want to create blank record on the database? You can do like this :
The user choose the number of new record he wants then, he submits
You display the creation form X times
On general submit, you submit each form to your create action
I don't know if you know phpmyadmin, but they do the same.
A better approach is to create x numbers of new objects (ex. Post.new) in a loop/partial configuration. Doing a Post.new creates a new instance of the appropriate db row, but it stores that information in memory and doesn't create that object in the db until you issue the Post.save command. These virtual database fields have all of the appropriate columns so you will be able to pass them to an edit form and everything should work. The only thing that it's missing is an id which will be assigned to it after you save.

Mixing forms in Rails

In my app users can register information about a tournament. If the user is a paying customer, she can fill in additional info, otherwise the fields for the additional info are unavailable (visible but disabled).
I laid this out as two objects: Tournament and TournamentExtras where the former has_one :tournament_extras and the latter belongs_to :tournament. Tournament also accepts_nested_attributes_for :tournament_extras.
I would like this to show up as a single form. The fields in the two objects are related, meaning you can add a start date (to Tournament) but only paying customers can add an end date (to TournamentExtras) but since the two fields are logically realted, they should show up after eachother in the form.
How do I do this?
I tried opening the form_for and fields_for loops withing eachother (before adding any fields) hoping that I could add any field whereever I like. That didn't work; apparently you can't reference the form object within the fields loop(?).
form_form and fields_for should work with each other.
Somewhat of a skeleton for this is:
form_for(#tournament) do |f|
f.fields_for(#tournament.tournament_extra) do |g|
end
end

Resources