Select element, simple_form helper - ruby-on-rails

I am trying to create an element in my form that uses simple form+bootstrap. The idea is to allow a user select a type of currency from a drop down.
Customer_currency to select from either USD- US Dollars, LRD - Liberian dollar among others.
I have used the following in my form
However, it is not working, all I see is a drop down (out of position) in my form with the options but no label.
How can i create a good select element with a label using simple form
<%= simple_form_for #customer, :html => { :class => 'form-horizontal' } do |f| %>
<%= f.input :name %>
<%= f.select :customer_currency, [['UGX- Uganda Shillings',1],['USD- US Dollars',2]] %>
<%= f.input :payment_terms %>
<%= f.input :billing_address %>
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.input :mobile %>
<%= f.input :email %>
<div class="form-actions">
<%= f.button :submit, :class => 'btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
customers_path, :class => 'btn' %>
</div>
<% end %>

<%= f.input :customer_currency, :collection => [['UGX- Uganda Shillings',1],['USD- US Dollars',2]] %>

Add label_method and value_method to your select, means change:
<%= f.select :customer_currency, [['UGX- Uganda Shillings',1],['USD- US Dollars',2]] %>
to:
<%= f.select :customer_currency, [['UGX- Uganda Shillings',1],['USD- US Dollars',2]], label_method: :first, value_method: :last %>
Update: other solution
<%= f.input :customer_currency, as: :select, [['UGX- Uganda Shillings',1],['USD- US Dollars',2]], label_method: :first, value_method: :last %>

Related

How do I add an autocomplete="street-address" attribute to rails form_for

I'm trying to add Mapbox autofill to my address bar on rails, for mapbox autofill to work I need to have autocomplete="street-address in an input field.
I'm using simple_form_form and I can't figure out how to add autocomplete attribute. Here is my form:
<%= simple_form_for #lock do |f| %>
<div class="form_wrapper">
<div class="form_div">
<%= f.input :name %>
<%= f.input :description %>
<mapbox-address-autofill>
<%= f.input :address, :autocomplete => "street-address" %>
</mapbox-address-autofill>
<%= f.input :photo, as: :file, label: "Link to picture" %>
<%= f.input :special_content %>
<%= f.button :submit, :class => "btn btn-primary" %>
<% end %>
any ideas on how to make the autocomplete attribute work?
Use input_html and pass the additional options like class etc.
<%= f.input :address, input_html: { :autocomplete => "street-address"} %>

Checking simple_form f.input value before submitting the form

I have this form:
<%= simple_form_for(#student) do |f| %>
<%= f.input :first_name, label: "Nombre" %>
<%= f.input :hours_studying, as: :select, collection: [1, 2],
prompt: 'Horas' %>
<%= f.input :library_id, as: :select, collection: #libraries %>
<div class="text-center">
<%= f.button :submit, "RegĂ­strame!", class: "btn button rounded-pill
p-3 my-4 fs-18 shadow" %>
</div>
<% end %>
Libraries have a country and a city, how could I add two fields to the form to check the country and the city and filter the libraries accordingly?

Filter cities by state in Simple_form Ruby on Rails

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) %>

How to submit a form using simple_form_for when what's being submitted isn't part of the model

I'm trying to submit a form that contains fields for both an event and an invitation to that event. Here is my form:
<%= simple_form_for(#event) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :title %>
<%= f.input :description %>
<%= f.input :start_at %>
<%= f.input :end_at %>
<%= f.input :all_day %>
<%= f.hidden_field :owner_id, value: current_user.id %>
<% if false %>
<%= f.association :sitter, label_method: lambda { |s| "#{s.name}" }, collection: User.all %>
<%= f.association :group, label_method: lambda { |g| "#{g.group_name}" }, collection: Group.where(:owner_id => current_user.id) %>
<% end %>
<%= f.input :user_emails, as: :text %>
<%= form_tag event_invitations_path, :method => :post do %>_tag :user_emails %>
</div><div>
<%= label_tag "Your message:" %>
</div><div>
<%= text_area_tag :email_message %>
<% end %>
</div>
</br>
<div class="form-actions">
<%= f.button :submit, :class => 'btn-primary' %>
</div>
<% end %>
This is (perhaps obviously) not working. :user_emails is NOT part of the event or invitation model as it's a list of emails that will be used to create invitations. Basically I merged two forms, one that was accepting the email/send invitation piece and one that was accepting the event information. I think I have my controller and model set up properly to take care of this but how do i submit this information as part of the same form without getting an "undefined_method" error (since user_emails doesn't belong to events). Let me know if you want to see my model/controller.
Use FormTagHelper
<%= label_tag "User", "Email"%>
<%= text_area_tag "user_emails", "example#example.com"%>
See the documentation for more options on text_area_tag and label_tag

Rails 4 simple_form collection_select

How do I translate the following to simple form?
<%= form_for(#bill) do |f| %>
<%= f.label :location_id %>
<%= f.collection_select(:location_id, #locations, :id, :location_name,
{:selected => #bill.location_id}) %>
I can't use a simple association because #locations is the result of a where query.
Try this
<%= simple_form_for #bill do |f| %>
<%= f.input :location,:collection => #locations,:label_method => :location_name,:value_method => :id,:label => "Location" ,:include_blank => false %>
<%= f.button :submit %>
<%end%>
Hope this help
Here is the final:
<%= f.input :location_id, collection: #locations, label_method: :location_name, value_method: :id,label: "Location", include_blank: false, selected: #bill.location_id %>
In simple form, if we have an association between location and bill, than we can do like this:
<%= simple_form_for #bill do |f| %>
<%= f.association :location, collection: #locations, priority: #bill.location %>
<%= f.button :submit %>
<% end %>
Hope this will help.
In your location model, you can also create a :
def to_label
location_name
end

Resources