I have a selection option in my form that I know is deterring the form from submitting. I can't figure out why though. Here is the snippet that is causing an issue:
<%= f.select(:state_id, options_for_select(State.all.map {|p| [ p.home_state, p.id ]}), :label => "State", :class => "dropdown-menu")%>
Here is the rest of the form:
<% provide(:title, 'New Profile') %>
<%= bootstrap_form_for(#profile) do |f| %>
<%= f.text_field :name, :autofocus => true, :placeholder => 'Name' %>
<%= f.number_field :age, :placeholder => 'Age' %>
<%= f.form_group :sex, label: { text: "Sex" } do %>
<br>
<%= f.radio_button :sex, 'Male', label: "Male", inline: true %>
<%= f.radio_button :sex, 'Female', label: "Female", inline: true %>
<% end %>
<%= f.text_field :city, :id => 'gmaps-input-address', :placeholder => "City" %>
<%= f.select(:state_id, options_for_select(State.all.map {|p| [ p.home_state, p.id ]}), :label => "State", :class => "dropdown-menu")%>
<%= f.submit "Submit", :class =>'btn btn-primary' %>
<% end %>
Any help is always much appreciated.
If anything, you should look at the collection_select helper:
<%= bootstrap_form_for(#profile) do |f| %>
<%= f.text_field :name, autofocus: true, placeholder: 'Name' %>
<%= f.number_field :age, placeholder: 'Age' %>
<%= f.form_group :sex, label: { text: "Sex" } do %>
<%= f.radio_button :sex, 'Male', label: "Male", inline: true %>
<%= f.radio_button :sex, 'Female', label: "Female", inline: true %>
<% end %>
<%= f.text_field :city, :id => 'gmaps-input-address', placeholder: "City" %
<%= f.collection_select :state_id, State.all, :home_state, :id, prompt: "State", class: "dropdown-menu")%>
<%= f.submit "Submit", class: 'btn btn-primary' %>
<% end %>
You'll also want to post any console logs you may have received for the request. Remember that computers are logic-based, they're not emotional and will only stop working if there's an error. Your job is to find the error and fix it, hence showing what's going on with your code infinitely helps, where guesswork doesn't :)
Related
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.
I'm using bootstrap_form gem for form generating in Rails. In my form view, I am using form_tag instead of form_for
<%= bootstrap_form_tag url: 'metafields' do |f| %>
<%= f.hidden_field :owner_id, value: params[:owner_id] %>
<%= f.text_field :namespace, placeholder: 'Namespace of metafield' %>
<%= f.text_field :key, placeholder: 'Key of metafield' %>
<%= f.text_area :description, placeholder: 'Description about metafield' %>
<%= f.select :type, [['String', 'string']], class: 'selectpicker' %>
<% end %>
Is there a way to submit this form with params including prefix namespace like params[:metafield][:owner_id].
When I tried with
<%= bootstrap_form_tag url: 'metafields', namespace: 'metafield' do |f| %>
It generates below input, which is not what I want
<input placeholder="Key of metafield" name="key" id="metafield_key" class="form-control" type="text" kl_vkbd_parsed="true">
quyetdc's answer is a way to go unless you have dynamic forms, here is a cleaner way
<%= bootstrap_form_tag url: 'metafields' do |f| %>
<%= f.fields_for :metafield do |mf| %>
<%= mf.hidden_field :owner_id, value: params[:owner_id] %>
<%= mf.text_field :namespace, placeholder: 'Namespace of metafield' %>
<%= mf.text_field :key, placeholder: 'Key of metafield' %>
<%= mf.text_area :description, placeholder: 'Description about metafield' %>
<%= mf.select :type, [['String', 'string']], class: 'selectpicker' %>
<% end %>
As from this, form_tag does not support namespace like form_for. Thus, I found a workaround as below, which generate correct name for input as if we use form_for
<%= bootstrap_form_tag url: 'metafields' do |f| %>
<%= f.hidden_field :owner_id, value: #owner_id %>
<%= f.hidden_field :owner_class, value: #owner_class %>
<%= f.text_field :'metafield[namespace]', label: 'Namespace', placeholder: 'Namespace of metafield' %>
<%= f.text_field :'metafield[key]',label: 'Key', placeholder: 'Key of metafield' %>
<%= f.text_field :'metafield[value]',label: 'Value', placeholder: 'Key of metafield' %>
<%= f.select :'metafield[value_type]', [['String', 'string']], label: 'Type', class: 'selectpicker' %>
<%= f.text_area :'metafield[description]', label: 'Description', placeholder: 'Description about metafield' %>
<%= f.submit 'Submit', class: 'btn btn-outline-dark float-right' %>
<% end %>
I have a drop down selector in a form and even after saving an object, when I go to my edit page, the drop down reverts to the first item. If submit is clicked, the value changes to the first item in the list.
In this case the drop down contains a list of states. Every time I go to the edit page, Alabama is selected and if I don't manually change the value back to what it initially was, the state becomes Alabama.
<%= simple_form_for #event, url: coin_event_path(#coin.id) do |f| %>
<%= f.input :content, :label => "Event Description", class: 'form-control' %>
<%= f.input :link, :label => "Link to Event", class: 'form-control' %>
<%= f.input :date, order: [:month, :day, :year], class: 'form-control' %>
<%= f.input :time, as: :time, html5: true, class: 'form-control' %>
<%= f.input :city, class: 'form-control' %>
<%= f.select :state, options_for_select(us_states),{}, class: 'form-control' %>
<%= f.input :description, :label => "Event Description", class: 'form-control' %>
<% if can? :destroy, Event %>
<%= f.select :accepted, [['Accepted', true], ['Not Accepted', false]] %>
<% end %>
<%= f.button :submit, 'Submit' %>
<%= link_to "Back", coin_path(#coin.id), class: "btn btn-default" %>
How do I change this so it stays on the state that it's supposed to?
You can using selected, like:
options_for_select(us_states, selected: "set_current_value")
More usage examples - options_for_select() docs.
I'm using simple_form to create my scaffolding forms in my current project. I need some help to improve my address CRUD. After my db:seed there are lots of cities registered.
My current form for address is:
<%= simple_form_for #address, :html => { :class => 'form-horizontal' } do |f| %>
<%= f.input :street%>
<%= error_span(#address[:street]) %>
<%= f.input :zip%>
<%= error_span(#address[:zip]) %>
<%= f.label :city_id %>
<%= f.collection_select(:city_id , City.all, :id, :name, prompt: true) %>
<%= error_span(#address[:city_id]) %>
<%= f.button :submit, :class => 'btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
address_path, :class => 'btn btn-default' %>
<% end %>
How can I filter my citties collection bellow selecting first the state ?
<%= f.collection_select(:city_id, City.all, :id, :name, prompt: true) %>
Thanks
Add this class method to your model:
def self.by_state
order('state DESC')
end
Then use it in your form:
<%= f.collection_select(:city_id , City.by_state, :id, :name, prompt: true) %>
I want to add delete button in actions. but all it does is js goback. I tried <%= f.actions %> didn't show the delete button. below is my effort to add it manually.
<% if can? :update, #parking_branch %>
<%= semantic_form_for #parking_branch do |f| %>
<%= f.semantic_errors %>
<%= f.inputs do %>
<%= f.input :parking_company_id, :as => :select, :collection => Hash[ParkingCompany.all.map {|c| [c.company_name,c.id]}], :required => true %>
<%= f.input :branch_name, :required => true %>
<%= f.input :email, :required => true %>
<%= f.input :telephone, :required => false %>
<%= f.input :latitude, :hint =>"Automatically filled based on address" %>
<%= f.input :longitude, :hint =>"Automatically filled based on address" %>
<%= f.input :airport, :required => true %>
<%= f.input :address1 %>
<%= f.input :address2, :required => false %>
<%= f.input :address3, :required => false %>
<%= f.input :city %>
<%= f.input :county %>
<%= f.input :postcode %>
<%= f.input :country, :as => :country, :priority_countries => ["United Kingdom"], :required => true %>
<% end %>
<br />
<%= f.actions do %>
<%= f.action :submit, :button_html => {:class => 'btn-primary', :disable_with => 'Please Wait...' } %>
<%= f.action :cancel, :button_html => {:class => 'btn-danger', :disable_with => 'Please Wait...', :method => :delete } %>
<% end %>
<% end %>
<% else %>
<br />
<h1> You are not authorised to do this! <h1>
<% end %>
I have destroy action in my controller too