The background is this:
our end users are not the most tech suave so we try to make it as simple as possible for these guys.
We have a form where a reviewer will look at a record and qualify it. This data goes into multiple columns:
example:
Was it a lead?
based on the answer it'll bring yup 2 select tags that'll go to the following database column:
yes:
<td><%= f.select :reasoninquiry_id, options_from_collection_for_select(#inquiryreasons, 'id', 'reason', #lead.reasoninquiry_id), { include_blank: true } %></td>
No:
<%= f.select :nonleadaction_id, options_from_collection_for_select(#nonleadactions, 'id', 'non_lead_action_type', #lead.nonleadaction_id), { include_blank: true } %>
We give our end users the opportunity to update this if they want. And here is where it gets tricky.
Currently our app is written in PHP and we use 1 select box and it sends data to 2 different tables, depending on what was selected.
Is there a way I can do this in rails?
To be clear what we'd want is 1 select box that will be able to submit data to 2 database columns, depending on what was selected. I'd want one select tag for 2 collections.
The select box would show options for both #inquiryreasons (which would submit to :reasoninquiry_id if one of them is selected) and #nonleadactions (which would submit to :nonleadactions_id if one of them is selected.)
Related
Still pretty new to ruby.. How would I add a feature to this drop down to allow users to enter an attribute that is not in the drop down list. Right now when I try to add something it says no results matched.
<%= f.select(:attribute_type, #attribute_types, {include_blank: true}, class: 'form-control selectpicker', data: {'live-search' => 'true'}) %>
The no results matched message is because you are not adding something to the select, but you are searching in the select list.
If you want users to have the option to fill in whatever they want instead of selecting it drop the select you can just add an input field below the select.
I have a search field where I want to add two dropdowns that should help me filter my search results. In my database I have one Product model with two columns, named country and type.
The first dropdown should filter by country and the second should filter by type. However, I am trying to set the second dropdown dynamically meaning that depending on the first dropdown (country) only those types should be displayed in the second dropdown that match the respective country selected. If possible this behavior should also work the other way around i.e. depending on the type, only matching countries should be displayed.
I've been trying several tutorials that work with more than one model however, I didn't manage to get it to work with only one model. My code for the dropdown selections is the following:
<%= form_tag("filter", :id => "filter_form", :method => "post") do %>
<label for="country" class="country">Country</label> <%= select_tag :country, options_for_select(Product.pluck(:country).uniq), { include_blank: 'Select country' }%>
<label for="type" class="type">Type</label>
<%= select_tag :type, options_for_select(Product.pluck(:type).uniq),{ include_blank: "Alle Kategorien" }%>
Is is possible with only one model?
Do I need AJAX for this or is Javascrip/jQuery sufficient?
Thank you very much!
I thought this would be fairly easy, but I'm not finding any help by Googling.
I have a form (simple_form) with numerous inputs with select lists (collections) that are populated from the database, so many it is slowing down the initial page load. I thought I could speed it up by only populating those drop down lists as the user selects them using Ajax. Is there something built in like remote => true for the form itself? Can someone point me in the right direction?
EDIT:
I found this SO question but I cannot figure out how to implement the answer.
Currently, my form looks like this;
= simple_form_for(#account)
= f.input :account_number
= f.input :area, collection: #areas
= f.submit nil, :class => 'btn btn-primary'
Based on the answer in the linked question, I should add something like this, but of course it is not working
= simple_form_for(#account)
= f.input :account_number
= f.input :area, collection: #areas, :input_html => {"data-remote" => true, "data-url" => "/my_areas", "data-type" => :json}
= f.submit nil, :class => 'btn btn-primary'
I can think of two ways to go about this if you don't want to load the contents initially when the page loads. One way is to run a script after the DOM has loaded to change the options for the select tag and the other is to collect the options when you click on the drop-down on the select element. I might go for the first way because there wouldn't be latency when a user clicks on the select element--they wouldn't have to wait for the options to populate.
So you'd run a jQuery script on document ready that makes an AJAX call to a method in your controller, which then returns the collections you want, then you iterate through the select elements you want to change with JQuery scripts. It might look something like this.
# in view with the select options to be changed
$(document).ready(function() {
$.get(change_selects_path, function(response) {
$.each(response, function(args) {
// code for each select element to be changed
$('.class_of_select_element').html(<%= j options_from_collection_for_select(args) %>);
});
});
)};
# in controller
def change_selects
# make db calls and store in variables to feed to $.get request
end
Note that this not tested but should give you a good start towards a solution. For further info on the each loop, you can check out this documentation.
Not sure if this fits your exact use case (please clarify if not), but I also have a few collection selects that have a large amount of database rows behind them. I use the select2-rails gem to take care of this. Users can begin to type in the name and the relevant results will show up (it will also show a few initially if they don't type something).
Check it out here: https://github.com/argerim/select2-rails
Edit: For a cascading dropdown, I recommend this gem: https://github.com/ryanb/nested_form
I am sorry if i cannot make you understand.
I have two tables where the first table contains categories and the next one contains sub-categories and i have another table item where i have has_and_belongs_to_many relation with sub-category table. When i create a new item, i have to display the categories in the first select box and based on the selection, the sub-categories has to be displayed in another select box and the value has to be added. I have already another like shown below
<%= collection_select :item, :sub_category_ids, SubCategory.find(:all, :conditions => ["category_id = 7"], :order => 'sub_category_name ASC'), :id, :sub_category_name, { :selected => #item.sub_category_ids }, { :multiple => true, :name => 'item[sub_category_ids][]' } -%>
and now i want to add this one like
along with that. How can i do this?
You'd have to use AJAX to achieve what you want.
You would want to use Javascript to watch the first drop down value. When that value changes, you would want to make an ajax call to retrieve the values of the second drop down, then fill the second dropbox based on the AJAX response
I have a question with regards to this tricky situation. I have code for a single select box where I can select a year, and based on the year, I want to return all possible values pertaining to that year in another select tag....
<%= select "month_end", "year", MonthEnd.find(:all, :select => 'DISTINCT year', :order => 'year').collect{|x| [ x.year, x.id ]} %>
I've tried to use observe field but it doesnt work, I need some help please.
Thank you very much for any help, have a great x-mas/new years
i have had the same idea : populating a select_tag based on result from another select_tag.
The solution i had was : showing one select_tag and using ajax to send the choice of the user to an action in the controller. The response sent by this action was the list of the choices that the second select will use.
The second select will be rendered by a partial and this way you can gave a more nice User Exprience .