Select or input - ruby-on-rails

In my app I have a form for creating new trip. Model Trip has field finish_address. For setup the finish_address in I use select in simple form to select address from user's addressbook. But I want to make a better form: if there isn't necessary address, user can add it using input field.
So I need to make form with to types of setup finish_address. How can I make it?

You have 2 basic options.
You can use autocomplete on a text input, populating a dynamic crop-down with know values.
There are several gems available to get you started in this direction, like https://github.com/crowdint/rails3-jquery-autocomplete.
Alternatively, you can add an "other" option to a select input, populated with your known values. When the "other" item is selected, display a previously hidden text input with the same name below the select input. The "lowest" element will take precedence, for what gets sent to the controller.
In the controller, just do a find_or_create_by, using your provided value.
These options both require javascript, but you can eliminate the need for javascript if you make your select a non-db-backed attribute, and manipulate your params accordingly, as they come in to the model. This might help with validations, as well.

Related

Can you include an input field in a drop down list using select-2?

I am using select-2 for a list of states. I think its great and love the search input field it provides. I'm wondering though, is it possible to include another input so someone can manually enter information ~ I am hoping to add an "Other" or "International" field to this drop down so instead of including all the states / provinces / territories of the world I provide a field they can manually enter if the territiry if it's not included in the drop down list.
Is an input in a drop down even possible? I would like to try and stick with Select2 as I like the added features it provides.
You can add items manually to the select2 dropdown using the "tagging" function. See the link for more information: https://select2.org/tagging

Place Autocomplete in iOS (swift) on Signup (address) form

I am a signup form which has bunch of text fields like name, phone, address 1st line, city, state etc.
Can I use googles's Place Autocomplete on this form in such a way that when I start typing address 1st line in one of the fields it suggests/auto completes and fills the city and state
1st question, is it possible, if yes are there any examples or tutorials?
There are examples out there but they use autocomplete UI, I want to be able to use my own edit text fields.
So Yes I was able to do exactly what I wanted.
I had two dependencies for this.
1. AutoComplete Text Field
2. Google's Place AutoComplete

ios/xcode/coredata: How to mimic ajax call in objective c

For a tag system, when entering a new item, I'd like the user to start typing a letter or two and see possible tags...similar to how SO tags work on the website i.e. Ajax.
Is there a way to do this on IOS?
Basically the Add Item screen has some empty text fields where you put the name etc.
I'd like to have an additional empty field where as you enter letters you see possible tags appear below and can then select one to tag the item.
The tags would be served from an entity or table so there would have to be a call to core data to supply them based on the letters typed.
Do I have to implement a full blown tableview to do this? Or is there a way to make the possible tags show up below the textfield box.
Thanks for any suggestions.
You could try a third party development in order to make what you want. In a recent project I have used this one:
https://github.com/EddyBorja/MLPAutoCompleteTextField

Fill a rails form with a hashmap

I have a difficult situation.
I let the the user create a form through a Rich Text Editor and then I save this.
So for example, I save this literally into my DB:
http://pastebin.com/DNdeetJp (how can you post HTML here? It gets interpreted, so now I use pastebin...)
On another page I wrap this in a form_tag and it gets presented as it should be.
What I want to do is save this as a template and save the answers as a hashmap to my DB.
This works well, but the problem is I want to recreate what checkbox/radiobutton/... is selected when the user goes back to the page. So I want to fill the form with the answers from the hashmap.
Is there a way to use a 'dummy' model or something else to accomplish this?
Thanks!
Since you're pasting in raw HTML which is not properly configured as a template, it is more difficult to enable the proper options based on whatever might be stored in your DB.
The reliable approach to making this work is to use Hpricot or Nokogiri to manipulate the bit of HTML you have and substitute values accordingly. This isn't too hard so long as you can define the elements in that form using a proper selector. For example, create a div with a unique id and operate on all input elements within it, comparing the name attribute with your properties. There may even be a library for this somewhere.
The second approach is to use JavaScript to enable the options in much the same fashion. This seems like a bit of a hack since the form itself will not have a proper default state.

Creating and editing my element instead of using `new`

So, basically my 'index' action is a long list of elements output as a table with Rails, each one has a checkbox next to it. These elements are items in a catalog that will be line-items in a quote.
A user can select as many checkboxes as they like, then click a button that says "Create a new Quote" it'll take the items they checked and clone them into a new table so they can edit them as apart of the quote.
The issue I'm having is that when the list is really long, say 6000 records it’s a n awfully long list to load, so I want pagination, BUT! then my checkboxes won’t work, because they will go away if the user changes pages.
Additionally the user has no way of going back to this page to "add" more items to the quote.
My first thought was to create a new element right when they browse to this "index page and essentially have the user editing the quote from the get go"
Any ideas on this implementation? I'm a little stuck on how to get started implementing this.
This is a classic shopping cart type problem. You might want to read up on implementing those in Rails with sessions. Simple example is to use the user's session. You can either create a class to encapsulate the logic, or just use a hash or array to hold the ids that are checked. So when the user clicks on "Start New Quote" you can set a var in their session to blank like:
session[:new_quote_items] = []
Then when they check off items you can have a link to add them to the quote. You can then place those ids into the array. On page display of the possible quote items you can show the user which ones are check and have then add/remove IDs from this list. Then when the user is done they can click on a finish link which will take the ids from the session and create the quote as you have now.
This question is similar to yours and covers your first problem. Pagination.
In addition to that, clever use of accepts_nested_attributes_for on the join model will allow you to simplify addition of elements to either existing or new quotes.
Sorry I've got no code for you right now, but here is the general idea:
Generate a list of QuoteItems that is is the union of the set of existing quote items for this quote, and quote items built for the current quote from items not part of the current quote.
Give that list to fields_for and set up each field_for region so that it passes the item_id as a hidden value and contain a checkboxes, such that they modify the :_delete field, with default value evaluating true, and checked value equating to false.

Resources