Rails - turn off autocomplete for text_field and password - html

I just give up. I have remembered password and login for my phpmyadmin on www.xxx.com/phpmyadmin like:
root
asdfasfsdfs
Then on my aplication on www.xxx.com/model/new , I have
<%= form_for #model do |f| %>
<%= f.text_field :city, :placeholder => 'Write Your city' %>
<%= f.password_field :password, :placeholder =>'SET PASSWORD' %>
<% end %>
Why browser put in text_field for city "root" and in password - password for root of phpmyadmin?
How can I remove autocomplete of this fields? I try:
autocomplete => 'off'
autofocus => false
:value => ''
$('#field').val('')
Nothings work... Peoples do not see placeholder text, because fields are filled ...
ANSWER:
Ok, I make by jQuery, like this:
$(window).load(function(){
$('.div_class input').val('')
});
So fields are filled but after load is done jQuery clear them.

just change the syntax a little. This worked for me :autocomplete => :off
<%= f.text_field :city, :placeholder => 'Write Your city', :autocomplete => :off %>

You can turn off the feature by setting the autocomplete attribute to off in the form or field.
<%= f.text_field :city, :placeholder => 'Write Your city', :autocomplete => 'off' %>
However, keep in mind that the directive is not compatible with all browsers and major browsers are moving towards ignoring the attribute for password fields, (e.g Firefox.)

NOTE: DON'T FORGET TO RESTART THE SERVER AFTER THIS CHANGE
<%= f.text_field :city, :placeholder => 'Write Your city', :autocomplete => 'off' %>

Related

Rails: How to make text field editable only once?

I'm making a little form and what I'm trying to do is to make one of the text fields get the value only when notification is created. I have:
<%= f.label :User_ID %>
<%= f.text_field :user_id, :value => current_user.id ,:readonly => true %>
My problem is that when other users are editing notifications, then this text field is automatically changing it's value. I'm not sure how to make it work. Thanks!
In your view you could check if the user id is already present and if so, don't display the user id text_field:
<% unless user_id.exists? %> #only if user_id is not present, show the text_field
<%= f.text_field :user_id, :value => current_user.id ,:readonly => true %>
<% end %>
Another option would be to add a boolean value to the model you are referring. For example, a notification model:
notification: string
edit: boolean (default: true)
After a user creates a notification, you set the boolean value for "edit" to false with an after create for example.
The next time a user edits that notification, you do the same in the view as before:
<% unless #notification.edit? %> #only if edit is set to true, show the text_field
<%= f.text_field :user_id, :value => current_user.id ,:readonly => true %>
<% end %>
It is a little vague but it gives you an idea how to do it.

simple_form: disable form without adding disabled: true or readonly: true to every input

I have a large simple_form form with fields that need to be enabled or disabled depending upon where the form's partial gets loaded.
My question is: how do you disable every form input quickly using simple_form helpers / wrappers?
Simple Form's documentation explains how disabled: true can be used to disable a single input field:
<%= simple_form_for #user do |f| %>
<%= f.input :username, disabled: true %>
<%= f.button :submit %>
<% end %>
But the documentation is less clear on how I can disable an entire form via simple_form helpers without needing to repeat disabled: true on literally every form input.
I tried passing disabled: true and readonly: true to simple_form's :wrapper_mappings option, but that isn't working.
Example Code:
I load the form via a partial to define simple_form display variables. This works:
#user/show.html.erb:
<%= render partial: 'shared/form', locals: {questionnaire: #questionnaire, readonly_state: true, disabled_state: true, bootstrap_form_class: 'form-horizontal'} %>
However, readonly_state and disabled_state do not work unless I pass them to every form input:
# shared/_form.html.erb
<%= simple_form_for(#questionnaire, :html => {:class => bootstrap_form_class},
:wrapper_mappings => {check_boxes: :vertical_radio_and_checkboxes, file: :vertical_file_input,
boolean: :vertical_boolean }) do |f| %>
<%= f.input :username, disabled: disabled_state, hint: 'You cannot change your username.' %>
<%= f.input :email, disabled: disabled_state %>
<%= f.input :city, disabled: disabled_state %>
<%= f.input :country, disabled: disabled_state %>
. . .
<%= f.button :submit %>
<% end %>
You can quickly see how repetitious this gets with a large form.
How do I toggle disable and readonly form attributes quickly across an entire form using DRY code?
You can create a custom wrapper that disables the input like this:
# config/initializers/simple_form.rb
config.wrappers :disabled_form do |b|
b.use :input, disabled: true, readonly: true
end
And in the form use:
<%= simple_form_for #model, wrapper: :disabled_form %>
<%= f.input :field %>
...
<% end %>
Depending on the amount of different inputs in the form, you might need to create more custom wrappers and use a wrapper_mapping in the disabled form.
Funny:
<%= f.input :username, disabled: true %>
Generated 'disabled' class for element.
<%= f.input :username, input_html: {disabled: true} %>
Don't do this :)
But you can do these:
<%= f.input :username, input_html: {readonly: :true} %>
or
<%= f.input :username, input_html: {disabled: :true} %>
Where (unlike readonly) the cursor changes to 🛇
Just a suggestion, you can achieve the behaviour using jquery by setting $('.form input').prop('disabled', true); where form is your form class.
You can use CSS to mimic the disabled property.
Just create the class and add it conditionally.
.disable {
background: #f2f2f2;
pointer-events:none;
}

Ruby on Rails: How to purge entries from search box

I am using a search box using the following form:
<div id='search-box'>
<%= form_tag "/search", :method => :get, :id => "search-form" do %>
<%= text_field_tag :q, params[:q], {:id => "search-text", :placeholder => "SEARCH FOR PRODUCTS"} %>
<%= submit_tag "Go" %>
<%end%>
</div>
The problem is that whenever I type something in the search box, it shows a dropdown with previous queries. I do not want to store the queries and show the dropdown. How can I purge the queries?
use autocomplete="off":
<%= text_field_tag :q, params[:q], {:id => "search-text", :placeholder => "SEARCH FOR PRODUCTS", :autocomplete => 'off'} %>

Rails collection_select custom name attribute

I have the following collection select which acts as a filter in a Rails app.
<%= form_tag( "/appointments", :method => "get", :id => "filter_form") do %>
<%= collection_select :doctor, :id, #doctors, :id, :full_name, {:include_blank => 'All'} %>
<% end %>
This always generates a name attribute of the select element like name="doctor[id]" which results in the browser to ?utf8=✓&doctor%5Bid%5D=1, which is not quite readable.
How can I change the name attribute to just name = "doctor" or basically just remove the brackets from it?
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select
The collection_select method contains the parameters "options" and "html_options". "options" allow you to add specific information, like {:include_blank => 'All'}, but does not replace html attributes.
You have to add the name to the next hash, like this:
<%= form_tag( "/appointments", :method => "get", :id => "filter_form") do %>
<%= collection_select :doctor, :id, #doctors, :id, :full_name, {:include_blank => 'All'}, {:name => 'doctor'} %>
<% end %>
Have you tried:
<%= form_tag( "/appointments", :method => "get", :id => "filter_form") do %>
<%= collection_select :doctor, :id, #doctors, :id, :full_name, {:include_blank => 'All', :name => 'doctor'} %>
<% end %>

log in page fails at form tag definitions

I have the following code snippet
<%= form_tag :action => 'process_login'%>
Username: <%= text_field "user", "fullname" %>
Password: <%= password_field "user", "password" %>
<%= submit_tag %>
<%= end_form_tag %>
that is used as an index.rb page to log in my system.
The controller index function looks like this
def index
#user=MyEmployee.new
#user.fullname=params[:fullname]
#user.password=params[:password]
end
The error is
undefined local variable or method `end_form_tag' for #<#<Class:0x5cbe468>:0x5cbcb60>
what should I add or change to make it work ?
UPDATE
And here is my process_login function
def process_login
if user=MyEmployee.authenticate(params[:fullname])
session[:id]=user.id
redirect_to session[:return_to]
else
flash[:error]='Login fails, unauthenticated user'
redirect_to :action => 'login', :fullname=>params[:user][:fullname]
end
end
You're using a very, very old syntax there. end_form_tag was deprecated in Rails 2.
Use this syntax instead:
<%= form_tag :action => 'process_login' do%>
Username: <%= text_field "user", "fullname" %>
Password: <%= password_field "user", "password" %>
<%= submit_tag %>
<% end %>
Whatever guide has you using end_form_tag is really old and shouldn't be used any more. I would recommend following the Rails Tutorial.