Rails: many-to-one display on form via dynamically added input text boxes - ruby-on-rails

I have a form in rails that allows the user to create a new object (call it a search).
This object has_many excluded_phrases.
What I would like is the ability to display one text box per excluded phrase added.
The form will start out with only a single text box, allowing the user to add one excluded phrase. If they want to add more, there will be button labeled "+" that will dynamically add one more text box, and allow the user to add as many more items as he wants.
On form submit, this should populate the db with the user search, and create all the required phrases that are linked to that user search.
Help?

All inspiration needed is there:
http://railscasts.com/episodes/197-nested-model-form-part-2

This popular rails plugin does exactly what you want.

Related

Rails admin cascading dropdown how to?

As you see in image below after selection of product attribute i want to filter the options based on the selected attribute_id
this is in rails_admin
I'm sorry to say there's no builtin way to do this.
You'll have to clone the existing belongs to field.
Check out the field implementation
https://github.com/sferik/rails_admin/blob/master/lib/rails_admin/config/fields/types/belongs_to_association.rb
And its view
https://github.com/sferik/rails_admin/blob/master/app/views/rails_admin/main/_form_filtering_select.html.haml
You'll find more details on how to create your own custom field here
https://github.com/sferik/rails_admin/wiki/Custom-field

Conditional show/hide fields based on show/hide links

What I try to do is very simple.
I have some fields in a form that are optional to fill. To make the form look cleaner, I'd like to show simple links as Add field and display the optional fields below via js unobstructedly.
If the field is present, it will be saved into the database. The field would also have a delete link. When this delete link is clicked, the field will be removed and also its value from the database when saved.
Seems pretty simple, yet I haven't found a simple approach to this. The whole solution involves many aspects such as passing hidden data to the controller to see if the record was hidden in order to delete its data from the database.
I would prefer a gem already optimized for this purpose but I couldn't found any. What I found is intended for associated records but this is not the case. These are not associations, just optional attributes from the same model.
nested_form_fields gem its also not suitable since it needs a checkbox or radio button to be triggered and not Add field/Remove field links which I believe look cleaner.
Is there such thing as a gem to accomplish this simple thing?
Instead of trying to send hidden data etc. Why don't you just load all the form fields but display:none. When a user clicks the 'show' button you display the field. If the click the hide button, just set the field to null and hide it.
When your form reaches the controller it will save the null value to the db for the hidden fields (same as if you deleted the value).

Raw HTML in Form Filter

At my company, we had developed a athletes management solution, were each athlete is inserted in the application by administrators users. For the referred solution, it was used the symfony admin generator.
On the second project iteration, one of the clients request was to turn the printed athletes list more legible. To accomplish that, we had created proper CSS styles, to be used when the user selects the browser print option.
However, the athletes form filter has some HTML tags (symfony widgets) whose do not render properly, namely, the select tag that has possible multiple choices (the select choices do not appear on the print preview).
So, I would to know if is possible to insert raw HTML directly on the form filter (get the selected options and convert them is labels or plain text), that will be only visible when the user selects the print option.
You could put a custom widget in place of the standard choice widget that adds in the values you want to be shown when printing but hidden with styles by default. Then you show them using your print style sheet.

How to get multiple and individual update actions on product lines in mvc?

I've got a list of products in an admin section of my website. I have the product title and then an icon for delete and an icon for set visible/set invisible. I have both icons wrapped in their own form elements so they fire seperate Actions. I now one to add some checkboxes to each line so I can do a bulk delete. Following this:
How to handle checkboxes in ASP.NET MVC forms?
I'd need to wrap the whole list in a form tag to return the checkbox values, but then I have the icons on each row wrapped in form tags. I'm not sure what to do or how to handle this so it all works. How can I maintain the line form tags but still get bulk update functionality?
It's probably going to require some JavaScript. I would create a form outside of the table with a hidden element. When a box is checked, update the hidden element to contain a comma-separated list of all the IDs for rows that have the checkboxes checked (this would be pretty straightforward with jQuery). When that form is submitted, parse the values of the hidden element and delete accordingly.
Another option is to wrap the whole thing in one form and make the names of the delete buttons unique, then check for that when the form is submitted. When doing a bulk delete, you'll have all the checkbox values submitted like normal and you'll know it's a bulk delete based on which button they pressed to submit the form.

What is the best way to handle repeating forms in MVC?

The best public example that I can think of off the top of my head would be the amazon shopping cart. Where you have a page that displays multiple distinct records that can have multiple distinct fields updated.
I can't put each one in a form tag because the user may modify more than one record and then submit.
I can't just update all the records that I get back because:
1. Performance
2. Auditing
3. If someone changed the record that the user 'didn't change' when they were viewing the page and then the user submits those changes would be overwritten.
So how to best handle getting the data back and then getting which records where changed out of that?
Is that clear?
Use binding! Don't be iterating the form collection in your actions.
Steve Sanderson wrote a blog post about how to do it. I wrote a blog post on how to do it with MvcContrib.FluentHtml. Both posts are very detailed and include downloadable code.
Generate your form in a repeater, and append an ID to the form elements that increments with each new form. Save the number of repeated form elements in a hidden field. Then in your controller, read the value of this hidden field - that'll be the number of forms to read. Then, in a loop, retrieve each form's fields by specifying the name of the field, plus the loop index appended to the name, as the key.
You can use some javascript logic to detect when a form's value changes, and update a hidden field in that form's section if that occurs; or you can hide the original values inside a hidden field with each form section (although I don't recommend this as too many fields / forms will bloat your page).
one (but not necessarily the best) approach is to store which items are changed in a js-variable or something on the client side as they are changed, and then only send the data that is actually different from what the user recieved.
and as Erik stated, you could use hidden form elements to make sure that it works without js as well.

Resources