I have a form:
<%= form_tag("/users/submit_users_form", method: "post", class: "form-container", :autocomplete => 'off')
which renders as:
<form class="form-container" autocomplete="off" action="/users/submit_users_form" accept-charset="UTF-8" method="post">
and then I have the text_field_tag and password_field_tag:
<%= text_field_tag 'your_email', '', :required => true, :autocomplete => 'off' %>
<%= password_field_tag 'your_password', '', :required => true, :autocomplete => 'off' %>
which render as:
<input type="text" name="your_email" id="your_email" value="" required="required" autocomplete="off">
<input type="password" name="your_password" id="your_password" value="" required="required" autocomplete="off">
(even the password value is set to "")
So I have set all to autocomplete off. Now when I visit the website I get this:
Any clue why the autocomplete is still on?
The chances are you have saved your login details to the browser for said site URL therefore it will always be in there by default. If you remove them from your browser stored passwords area it should be fine as you have autocomplete="off" in the <form> tag.
Related
Question? How do I get my form.submit button to use a specific route? Specifically, the user can fill out this form from any page, and it will submit to a desired controller.
ruby -v 2.3.0
rails 5.0
This form is a feedback form for users to submit feedback. The way it works is, a little icon is available to click so the user can fill out and submit this form from ANY page. The Problem is, unless the user is on the homepage (local/customers), for example, they're on post/13, the form tries to add its URL route on top of the example and I get a "no route matches" ...post/13/customers/questionaire.
This is my route.rb
post 'customers/questionaire' => 'customers#questionaire'
This is the form view
<%= form_for :anything, url: "customers/questionaire" ,multiple:
true do |form| %>
<div><%= form.label :email, 'E-mail:' %>
<%= form.text_field :email , placeholder: 'JohnDoe#yahoo.com' %>
</div>
<div><%= form.label :feedback, 'Type of feedback:' %>
<%= form.text_field :feedback, placeholder: 'Problem, Bug, Idea...' %>
</div>
<div><%= form.label :notes, 'Notes: (Required)' %>
<%= form.text_field :notes, class: 'notes', id: 'notes', placeholder: "Your Feedback" %>
</div>
<%= form.submit "Submit", class: "btn1", id: "button", disabled: true %>
<% end %>
I think you need something like this. Do http://localhost:3000/routes. You will get all the routes in your app
<%= form_with scope: :post, url: customers_questionaire_path do |form| %>
<%= form.text_field :title %>
<% end %>
May be this for lower rails version :)
<%= form_for :customer, scope: :post, url: customers_questionaire_path do |form| %>
<%= form.text_field :title %>
<%= form.submit %>
<% end %>
What matters is the action attribute of the form HTML element, or the formaction attribute of a button or input element.¹
In Rails it's defined like so:²
<%= form_tag("/search", method: "get") do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Search") %>
<% end %>
Which yields:
<form accept-charset="UTF-8" action="/search" method="get">
<input name="utf8" type="hidden" value="✓" />
<label for="q">Search for:</label>
<input id="q" name="q" type="text" />
<input name="commit" type="submit" value="Search" />
</form>
This will send the form data to the /search route.
With this approach, each form of your page will use a different route as target for form processing. You could alternatively use the same route and treat multiple use cases inside a single route, but that's not what you're asking.
Alternatively (or in addition to) you can also use the formaction attribute in buttons and inputs, in which case you override the form element's action attribute:³
<%= form_tag("/search", method: "get") do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Search") %>
<%= submit_tag("Search On Rails", formaction: search_on_rails) %>
<% end %>
Which yields:
<form accept-charset="UTF-8" action="/search" method="get">
<input name="utf8" type="hidden" value="✓" />
<label for="q">Search for:</label>
<input id="q" name="q" type="text" />
<input name="commit" type="submit" value="Search" />
<input name="commit" type="submit" value="Search On Rails" formaction="/search_on_rails" />
</form>
It's as if the first submit button had a formaction="/search" because it's ommited, therefore the action="/search" in the <form> is used.
Other approaches in this question's answers and this one.
For your use case, you'll have to make sure the route /customers/questionaire it's consistent in any URI level (absolute, not dynamic). I lack this particular knowledge in Rails to provide a failsafe solution for this case, although it seems it works as expected in the current default behavior.
So the mistake in your code is using url: with a relative URI when you really want an action: in this line:
<%= form_for :anything, url: "customers/questionaire" ,multiple: true do |form| %>
Use instead:
<%= form_for :anything, url: {action: "customers/questionaire"}, multiple: true do |form| %>
See this question and this one.
In addition to this, instead of hardcoding routes / urls in the code, there's the url_for helper: https://api.rubyonrails.org/v5.1.7/classes/ActionDispatch/Routing/UrlFor.html
What I'm trying to achieve is something like this in rails
https://jsfiddle.net/uxc45hcs/
But rails doesn't seems to support HTML attribute "list"
.col-md-2.execute_at_input = f.input :execute_at, label: "Execute Attt", placeholder: " In Minutes ", list: 'listid'
The above code outputs the following:
<input class="string optional form-control" placeholder=" In Minutes " type="text" value="" name="execute_at" data-validate="true">
Where as it should be:
<input class="string optional form-control" placeholder=" In Minutes " type="text" value="" name="execute_at" data-validate="true" list="listid">
Am I doing something wrong?
Something like this should work: (tested in rails 5)
<%= f.text_field :name, :class => 'form-control col-md-2', :placeholder => 'In Minutes', :list => "listid" %>
This works with text_field
.col-md-2.execute_at_input= f.text_field :execute_at, label: "Execute Attt", placeholder: " In Minutes ", list: 'listid'
This resulted in following output
<div class="col-md-2 execute_at_input">
<input id="execute_at" label="Execute Attt" list="listid" name="execute_at" placeholder=" In Minutes " size="30" type="text" maxlength="255">
</div>
If you are using Formtastic check for the syntax
I use this code to allow the user to send a mail to another user:
<%= button_to("Contact me","mailto:#{#formation.usr.email}?subject=#{#formation.name}", class: "fiche__detail__contact") %>
Which gives me the following html:
<form action="mailto:user#gmail.com?subject=SomeName" class="button_to" method="post">
<div>
<input class="fiche__detail__contact" type="submit" value="Me contacter" />
<input name="authenticity_token" type="hidden" value="cO0XDBcG4j0IfmDJV56sdYSfoLeV9NmhTd+bJu/ku+U=" />
</div>
</form>
And this king of email:
I tried to set the body of the mail using &body but it doesn't work. Also, set :authenticity_token => false is not working either.
How can I remove the token?
Just use mail_to:
<%= mail_to #formation.usr.email, "Contact me", :subject => #formation.name, class: "fiche__detail__contact" %>
I am trying to build a form that is going to suggest medicine name from database but will send ID of chosen medicine to create relation in my database.
I used autocomplete gem which works great, I also used their hint for getting ID out of elements name and this works fine as well but only to the point where there is ONE INPUT element on form. In my case I need 5 inputs and because of my code - it keeps overwritting element called my_medicine_id which causes that only one - last - element is being saved. Can you guys think of any solution for dynamically changing field name?
My PrescriptionsController
[...]
def new
#prescription =Prescription.new
5.times { #prescription.relations.build }
end
[...]
My view
[...]
<ol>
<%= f.fields_for :relations do |builder| %>
<%= builder.hidden_field :medicine_id, :id => "my_medicine_id" %>
<%= builder.autocomplete_field :medicine_name, autocomplete_medicine_name_relations_path, :id_element => '#my_medicine_id' %>
<% end %>
</ol>
[...]
generates final html:
<input id="my_medicine_id" type="hidden" rows="5" name="prescription[relations_attributes][0][medicine_id]"></input>
<input id="prescription_relations_attributes_0_medicine_name" type="text" rows="5" name="prescription[relations_attributes][0][medicine_name]" data-id-element="#my_medicine_id" data-autocomplete="/relations/autocomplete_medicine_name"></input>
<input id="my_medicine_id" type="hidden" rows="5" name="prescription[relations_attributes][1][medicine_id]"></input>
<input id="prescription_relations_attributes_1_medicine_name" type="text" rows="5" name="prescription[relations_attributes][1][medicine_name]" data-id-element="#my_medicine_id" data-autocomplete="/relations/autocomplete_medicine_name"></input>
<input id="my_medicine_id" type="hidden" rows="5" name="prescription[relations_attributes][2][medicine_id]"></input>
<input id="prescription_relations_attributes_2_medicine_name" type="text" rows="5" name="prescription[relations_attributes][2][medicine_name]" data-id-element="#my_medicine_id" data-autocomplete="/relations/autocomplete_medicine_name"></input>
<input id="my_medicine_id" type="hidden" rows="5" name="prescription[relations_attributes][3][medicine_id]"></input>
<input id="prescription_relations_attributes_3_medicine_name" type="text" rows="5" name="prescription[relations_attributes][3][medicine_name]" data-id-element="#my_medicine_id" data-autocomplete="/relations/autocomplete_medicine_name"></input>
<input id="my_medicine_id" type="hidden" rows="5" name="prescription[relations_attributes][4][medicine_id]"></input>
<input id="prescription_relations_attributes_4_medicine_name" type="text" rows="5" name="prescription[relations_attributes][4][medicine_name]" data-id-element="#my_medicine_id" data-autocomplete="/relations/autocomplete_medicine_name"></input>
So as you can see each time it overwrites element data-id-element="#my_medicine_id".
Solution by OP.
Solution found - moved inside of fields_for to a partial and used f.options[:child_index].
Fixed view file:
[...]
<%= f.fields_for :relations do |builder| %>
<%= render 'child_form', :f => builder %>
<% end %>
[...]
And partial _child_form.html.erb
<% #it=f.options[:child_index] %>
<%= f.hidden_field :medicine_id, :id => "my_medicine_id#{#it}" %>
<%= f.autocomplete_field :medicine_name, autocomplete_medicine_name_relations_path, :id_element => "#my_medicine_id#{#it}" %>
I have a form and inside the form there is a text_field_tag. It is written as this...
<%= text_field_tag "search", :placeholder => 'Enter search term...' %>
However, when the HTML is generated this returns in the page source...
<input id="search" name="search" type="text" value="{:placeholder=>"Enter search
term..."}" />
Why is it doing this and how do I fix it so that placeholder works?
The second parameter of the text_field_tag is the value. Give it nil to have it empty:
<%= text_field_tag "search", nil, :placeholder => 'Enter search term...' %>
And give it a String to have a default value:
<%= text_field_tag "search", 'Enter search term...' %>
Add an onclick event to empty it with jQuery:
<%= text_field_tag "search", 'Enter search term...', :onclick => 'if($(this).val()=="Enter search term..."){$(this).val("");};' %>
Edit 2016:
Nowadays, most of the browsers now support the HTML 5 placeholder, which allows us to do this in a cleaner way:
<%= text_field_tag 'search', nil, placeholder: 'Enter search term' %>
# which produces the following HTML:
<input type="text" value="" placeholder="Enter search term">
jsFiddle link