How do you ensure address form autofills correctly? - ruby-on-rails

I have a form in Rails, part of which asks for an address:
<%= f.fields_for :postal_address do |ff| %>
<%= ff.text_field :apartment_number %>
<%= ff.text_field :building_name %>
<%= ff.text_field :building_number %>
<%= ff.text_field :street %>
<%= ff.text_field :town %>
<%= ff.text_field :postcode %>
<% end %>
However, when I click the little autofill thing in Safari (below) the form is filled in incorrectly.
This is obviously wrong:
Is there any way I can instruct the browser to autofill the fields with the expected data? Or is it solely up to the browser?

It should work better if you add autocomplete attribute to inputs. Browser tries to guess about field's purpose from its name, but not sure if it knows "Building number" and "Town"
<%= ff.text_field :town, autocomplete: 'address-level2' %> # 'city' should work too
You can check list of available values in the docs

Related

Rails nested forms separated

I'm after some direction in created nested forms operating in their own forms (if that makes sense). I've created some diagrams to help explain what I'm after.
I have the nested forms working fine, I'm just interested to find out if the below is possible.
I know I don't have any code to show, but any guidance or assistance would be greatly appreciated as I'm not sure where to start.
Model
class General < ApplicationRecord
belongs_to :operation
belongs_to :report
end
Form
<%= form_with(model: general, local: true) do |f| %>
<h1>General</h1>
<%= f.text_field :first_name %>
<%= f.text_field :last_name %>
<%= f.collection_select :property_id, Property.all, :id, :name %>
<%= f.collection_select :field_id, Field.all, :id, :name %>
<h1>Operations</h1>
<%= f.fields_for :operations do |o| %>
<%= o.text_field :model %>
<%= o.text_field :type %>
<%= o.collection_select :status_id, Status.all, :id, :name %>
<% end %>
<h1>Reports</h1>
<%= f.fields_for :report do |r| %>
<%= r.text_field :first_name %>
<%= r.text_field :last_name %>
<%= r.text_area :comments %>
<% end %>
<%= f.submit 'Submit' %>
<% end %>
This is my standard form using nested forms for Operations and Reports resulting is something like this:
I'm looking to seperate out the Operations and Reports forms and place a link available in the General show route.
The user will click on the Operations link and bring up the nested form to edit.
I'm not sure if I understand you correctly, but I guess you can hide operations and reports using CSS, add two buttons and add an event listener on each of them to display operations form and reports form on click (changing the hidden nested forms CSS).

No input found for varchar

I am trying use nested models in my rails application but I have a little issue.
This is my view:
<%= simple_form_for #installation do |f| %>
<div class="field">
<%= f.label :x %><br>
<%= f.input :x %>
</div>
<%= f.simple_fields_for :address do |u| %>
<div class="field">
<%= u.label :street_address %><br>
<%= u.input_field :street_address %>
</div>
<% end %>
<% end %>
When I run, I receive this error <%= u.input_field :street_address %> -> "No input found for varchar", but when I change this peace of code to <%= u.input_field :street_address, :as => :string %> work. Why this happen?
The magic is simple form will automatic detect your data type and automatically pick a input control for it. For instance:
text => text_area
string => text field
boolean => checkbox
As the document described, there is not data type of varchar that simple form can understand autotically, so you need to specify the input type manually!
So you can use as: :string or as: :text to make it work!

Rails form tags

I have a form to allow the user to upload an image file, but i currently have it in text format, so they enter the file name of the image, but i would like to have it so they click a select button to upload the file rather than typing it in, and then when the file is selected from their hard drive, it will show in the textbox next to the button, thanks.
<%= form_for(#question) do |f| %>
<p>
<%= f.label :question %><br/>
<%= f.text_field :question, autofocus: true %>
</p>
<p>
<%= f.label :description %><br/>
<%= f.text_area :description, cols: 40, rows: 7 %>
</p>
<p>
<%= f.label :posted_by %><br/>
<%= f.text_field :posted_by, size: 5 %>
</p>
<p>
<%= f.label :image_file_name %><br/>
<%= f.text_field :image_file_name, size: 30, placeholder: 'GIF, JPG, or PNG file' %>
</p>
<p>
<%= f.label :posted_at %><br/>
<%= f.date_select :posted_at %>
</p>
<p>
<%= f.submit %>
<%= link_to('Cancel', questions_path) %>
</p>
<% end %>
You can use file_field instead of text_field which allows to upload a file.
<%= f.file_field :image_file_name,accept: 'image/png,image/gif,image/jpeg',:multiple => true %>
:multiple - If set to true, in most updated browsers the user will be allowed to select multiple files.
:accept - If set to one or multiple mime-types, the user will be suggested a filter when choosing a file. You still need to set up model validations.
For more details, see this API
An answer can be found here: https://stackoverflow.com/a/10526674/786709
(source: http://guides.rubyonrails.org/form_helpers.html#uploading-files)
Try to use the file_field instead of text_field.
<%= f.text_field :image_file_name, accept: 'image/png,image/gif,image/jpeg' %>
See more: FormHelper.html#method-i-file_field

Registration Form: Unable to Hide Field

In creating a new application I have used marklocklear example (https://github.com/marklocklear/devise_multi_tentant) for assistance in creating a multi-tenant environment. It works well.
The next thing I wanted to do I thought would be simple but has turned into quite the niggle for me.
All I wanted to do is rather than having the initial admin user have to enter an organization name (which is not necessary in my application), I wanted to hide that field.
Sign Up Form (that works):
<h2>Sign up</h2>
<% resource.organization ||= Organization.new %>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :email %><br />
<%= f.email_field :email %></div>
<%= f.fields_for :organization do |org| %>
<div><%= 'Organization or Company Name' %><br />
<%= org.text_field :name %></div>
<% end %>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<div><%= f.submit "Sign up" %></div>
<% end %>
<%= render :partial => "devise/shared/links" %>
So, I did this to try to hide the field:
<%= f.fields_for :organization do |org| %>
<%= f.hidden_field :name %>
<% end %>
But I get an error.
The baffling thing to me is that if I leave the field in, but do not enter a value everything still works properly -- this is unexpected. Since (as part of my troubleshooting) I removed the field completely -- and got an error message stating the form could not be processed because the organization part was blank.
Any assistance would be appreciated.
Max: Thank you. As you know, that worked like a charm. The field is now hidden and the form submits properly. Thanks.
<%= f.fields_for :organization do |org| %>
<%= org.hidden_field :name %>
<% end %>

properly storing date info using form_for

I have a form that allows a user to create an event. One of the fields that the user fills out is date. Currently this is the code for the date filed:
<%= u.label :date %> <%= u.text_field :date, :placeholder => 'mm/dd/yyyy' %>
For some reason, if the user types in 01/02/2012 it stores as dd/mm/yyyy, thinking the event is on Feb 1 instead of Jan 2nd. I've already configured my initializer files to display date/datetime the way I would like it to (as recommended on several posts here) but this is still an issue
Update - here is my full form:
<%= form_for(#party_profile) do |u|%>
<p>
<%= u.label :name %><%= u.text_field :name %>
</p>
<p>
<%= u.label :location %><%= u.text_field :location %>
</p>
<p>
<%= u.text_field :date, :placeholder => 'dd/mm/yyyy' %>
</p>
<p>
<%= u.label :password %> <%= u.text_field :password %>
</p>
<%= u.submit "Let's Party!", :class => "btn btn-primary" %>
<% end %>
I have tried replacing u.text_field with select_date as suggested below, but then I get an error:
undefined method `select_date' for #<ActionView::Helpers::FormBuilder:0x007f9e5e8c69d8>
<%= u.label :date %> <%= u.date_select(:date, :order => [:month, :day, :year]) %>
is the best I could come up - still not formatted exactly how I'd like
I use that for my dates:
<%= u.label :date %>
<%= u.date_field :date %>
Hope it helps =)
Consider using select_date, rather than a simple text field where the user can type in anything.
<%= u.select_date(:date, :order => [:month, :day, :year]) %>
http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-select_date

Resources