if statement based on form select rails - ruby-on-rails

I have a basic form:
<%=form_for #person do |f| %>
<%= f.label :ThingsWanted, "Stuff I need" %>
<%= f.select :ThingsWanted, ['cat', 'dog', 'purse', 'lipstick', 'fish'], { :include_blank => 'Selector' }, :required => true %>
<%= f.submit 'Done'%>
<%end%>
What I want to do is created an if statement based on what is selected before the form is submitted. Say lipstick is selected
Then I want to create a new form value called explain_yourself
<%if lipstick is selected%>
explain_yourself goes here
<%end%>
I don't know how to create the if-statement or rather access the select value before the form is submitted...

You can use jquery.
Add a class to the form and also to the input tag in question in the HTML
<%=form_for #person do |f| :class => "the_form_class" %>
<%= f.select :ThingsWanted, ['cat', 'dog', 'purse', 'lipstick', 'fish'], { :include_blank => 'Selector' }, :class => "the_input_class" :required => true %>
then in jquery, add an on-submit function to the form
$(".the_form_class").on("submit", some_function)
note: make sure you wrap this on-submit event handler in a document.ready
then define that function (the definition does not need to be wrapped in a document.ready)
function some_function(e){
e.preventDefault();
if( $(".the_input_class").val() == "lipstick"){
do_some_stuff
}
$(".the_form_class").submit();
}

This best workaround I found for this was using parameters and instance variables. Sorry the example is not great, but it works.
Do you have lipstick?
<%= link_to "Yes", onclick: #a=0%></td>
<%= link_to "No", onclick: #a=1%>
<% if params[:onclick].to_i == 1%>
<%= f.label :ThingsWanted, "Stuff I need" %>
<%= f.select :ThingsWanted, ['lipstick'], { :include_blank => 'Selector' }, :class => "the_input_class" :required => true %>
<%else%>
<br/>
<%= f.label :explain_yoself, "Select what you need" %>
<%= f.select :ThingsWanted, ['cat', 'dog', 'purse', 'fish'], { :include_blank => 'Selector' }, :required => true %>
<%= f.text_area :explain_yourself %>
<%end%>

Related

How do I make search from dataset using select2 gem quicker in my rails form?

There are over 9000 values in my dataset that users can choose from through a select tag. I am using select2 gem so they can search through the dataset but the time to load after and before i input something in the form is a lot. How do I reduce this? I alrady have minimuminputlength as 2 and that is not working for some reason.
<script>
$(document).ready(function() {
minimumInputLength: 2
$('.js-example-basic-single').select2();
});
</script>
<%= form_for #profile, url: user_profile_path, :html => { :multipart => true } do |f| %>
<%= f.text_field :first_name, placeholder: "First Name", class: 'input-1' %>
<%= f.text_field :last_name, placeholder: "Last Name", class: 'input-1' %>
<%= f.select :city, ['les Escaldes,Andorra,Escaldes-Engordany,3040051', 'Andorra la Vella,Andorra,Andorra la Vella,3041563', 'Umm al Qaywayn,United Arab Emirates,Umm al Qaywayn,290594'...], { :include_blank => 'City' }, :required => true, class: 'js-example-basic-single' %>
<% if current_user.profile %>
<%= f.submit 'Update', class: 'btn btn-default', class: 'bu' %>
<% else %>
<%= f.submit 'Create', class: 'btn btn-default', class: 'bu' %>
<% end %>
<% end %>
Also how I do edit the font and font size of the contents of the sleect 2 select values. Editing the class, 'js-example-basic-single' does not work.
Edit: The suggestion below does not work either for me. Am I doing something wrong?
<script>
$(document).ready(function() {
$('.js-example-basic-single').select2();
});
$('select').select2({
minimumInputLength: 3
});
</script>
<%= form_for #profile, url: user_profile_path, :html => { :multipart => true } do |f| %>
<%= f.text_field :first_name, placeholder: "First Name", class: 'input-1' %>
<%= f.text_field :last_name, placeholder: "Last Name", class: 'input-1' %>
<%= f.file_field :avatar, class: 'input-1'%>
<%= f.select :age, [13, 14, 15,16,17...], { :include_blank => 'Age' }, :required => true, class: 'input-1' %>
<%= f.select :gender, ['Male','Female', 'Other'], { :include_blank => 'Gender' }, :required => true, class: 'input-1' %>
<%= f.select :city, ['les Escaldes,Andorra,Escaldes-Engordany,3040051', 'Andorra la Vella,Andorra,Andorra la Vella,3041563', 'Umm al Qaywayn,United Arab Emirates,Umm al Qaywayn,290594'...],{ :include_blank => 'City' }, :required => true, class: 'js-example-basic-single' %>
<%= f.text_field :collegeemail, placeholder: "College Email (Leave Empty If You Do Not Have One)", class: 'input-1' %>
<% if current_user.profile %>
<%= f.submit 'Update', class: 'btn btn-default', class: 'bu' %>
<% else %>
<%= f.submit 'Create', class: 'btn btn-default', class: 'bu' %>
<% end %>
<% end %>
Seems to me you're putting all of the existing 9000+ cities from your db into the page. That's why it takes so long to open a select (it takes some time for user to download such page + it probably takes long to just open/render such select after the page is loaded).
You have to pass just a few options (cities in your case) on the page initially and fetch the rest using AJAX, see this instruction in the select2 docs.
You will need to implement an API endpoint in your Rails app to be able to fetch cities via AJAX request.

How to pre-select in options_from_collection_for_select?

I have the following form:
<%= form_tag users_path, method: :get, id: 'uco' do %>
<%= select_tag "country", options_from_collection_for_select(ISO3166::Country.countries.sort_by(&:name), 'un_locode', 'name'), :include_blank => true %>
<%= submit_tag "Search" %>
<% end %>
When I submit the form I end up with:
www.example.com/users?country=US
I would then like the form to pre-select params[:country].
However I do not know how to attach params[:country] into the select_tag. I unsuccessfully tried:
<%= select_tag "country", options_from_collection_for_select(ISO3166::Country.countries.sort_by(&:name), 'un_locode', 'name', params[:country]), :include_blank => true %>
based off of this example from the apidock.
Nevermind, I solved it with this answer:
How to make the select_tag keep value of last search?
:selected => params[:country]

How to get selected option in edit page for select menu

This is my select tag in ruby, after submitting the form ,i want to edit the selected option. so that i done <%= render 'form' %> in my edit.htm.erb ,but the option that i submitted is not appearing in the select field, when i click edit button. it shows select a category
Please suggest me a solution to solve this
form.html.erb
<%= simple_form_for([:coaches, #programme]) do |f| %>
<%= f.input :title %>
<%= select_tag 'category', options_from_collection_for_select(#categories, 'id', 'name',#categories.category_id), :class => "wrapper-dropdown-3_1", :onchange => 'update_subscategories_div(this.value)', prompt: "Select a Category" %>
<%= f.button :submit, "PUBLISH", :class => "btn_style" %>
edit.html.erb
<%= render 'form' %>
#categories = Category.all.map{|c| [c.name, c.id]}
<%= simple_form_for([:coaches, #programme]) do |f| %>
<%= f.input :title %>
<%= f.input :category, as: :select, collection: #categories, selected: f.object.category, input_html: { class: 'wrapper-dropdown-3_1'}, :onchange => 'update_subscategories_div(this.value)', include_blank: "Select a Category" %>
<%= f.button :submit, "PUBLISH", :class => "btn_style" %>
<% end %>
You need to pass id of selected element as third argument in options_from_collection_for_select method.
<%= select_tag 'category', options_from_collection_for_select(#categories, 'id', 'name',category_id), :onchange => 'update_subscategories_div(this.value)', prompt: "Select a Category" %>

Rails: How do I set up a collection_select?

I'm trying to set up a search / filter using collection_select.
Firstly: This works. It lists all cases for employee with id = 15.
<%= form_tag(cases_path, :method => "get") do %>
<%= hidden_field_tag :param_e, 15 %>
<%= submit_tag "Filter", :name => nil %>
<% end %>
But what I want is a collection_select, so I can list cases for any employee.
<%= form_tag(cases_path, :method => "get") do %>
<%= collection_select( :x, :y, Employee.all, :id, :name, {}, { :multiple => false }) %>
<%= hidden_field_tag :param_e, :z %>
<%= submit_tag "Filter", :name => nil %>
<% end %>
This shows the collection_select with all the employees in a drop-down.
How to I connect-up the collection_select?

Custom parameters from view to controller

i'm creating a form where a user can choose a product and a quantity. I need to pass the id value from the object #event to the controller, but i dont know whats the right way to do it. As it is now, it the params[:event_id] field is always nil in the controller.
<%= form_tag logic_giveRandomGifts_path :method => 'post' %>
<div class="form-group">
<%= collection_select(:params, :product_id, Product.all, :id, :name, :prompt => true) %>
Quantidate:
<%= text_field_tag :quantity, params[:quantity], :size => 2 %>
<%= submit_tag "GO!",params[:event_id] => #event.id,:class => 'btn btn-default' %>
</div>
Add hidden filed in your form and set its value equal to #event.id
<%= form_tag logic_giveRandomGifts_path :method => 'post' %>
<div class="form-group">
<%= collection_select(:params, :product_id, Product.all, :id, :name, :prompt => true) %>
Quantidate:
<%= text_field_tag :quantity, params[:quantity], :size => 2 %>
<%= hidden_field_tag :event_id, value: #event.id%> #add this
<%= submit_tag "GO!",params[:event_id] => #event.id,:class => 'btn btn-default' %>
</div>
<% end %>
You can use now event id in your controller as params[:event_id]
<%= hidden_field_tag :event_id, #event.id %>
So this will be available in params[:event_id].
Simply pass an hidden field in form as followings
<%= hidden_field_tag :event_id, value: #event.id%>
It will available in controller as
params[:event_id]

Resources