I have two lines
<%= f.text_field(:email, class: 'new_user_info', autofocus: true, placeholder: 'Email') %>
<%= f.text_field(:email_confirmation, class: 'new_user_info', autofocus: true, placeholder: 'Email Confirmation') %></td>
On http://edgeguides.rubyonrails.org/active_record_validations.html, it states that you can use the
confirmation: true
property with the following format
<%= text_field :person, :email %>
<%= text_field :person, :email_confirmation %>
I need the confirmation to work with the first format that I stated. However, I am unable to do so. When I add the confirmation: true property it is ignored by the validator. Could someone please tell me how to validate the fact that both emails are the same with Ruby on Rails using the first set of text_field tags that I gave?
Thanks for your time
Try using this instead
validates_confirmation_of :email
Related
From my understanding, empty form fields saving to database usually defaults to nil (which is what I wanted). But two of my optional form fields are saving as empty ("") instead of nil.
I'm not sure why this is, I've looked at: Understanding Rails validation: what does allow_blank do? and Save blank value as nil in database thinking it may be because I am using allow_blank: true.
My group.rb
# meetup_urlname must be unique
validates :meetup_urlname, uniqueness: { case_sensitive: false, message: "this is already being used by a different group" }, allow_blank: true
# only validate if meetup_apikey or if meetup_urlname is non-empty
validate :url_name_valid?, :if => :meetup_urlname?
validate :api_key_valid?, :if => :meetup_apikey?
_Form.html.erb:
<%= bootstrap_form_for(#group, :html => { :multipart => true }, layout: :horizontal, label_col: "col-sm-3", control_col: "col-sm-9") do |f| %>
...
<h5>Import Events from Meetup</h5>
<div class="field">
<%= f.text_field :meetup_urlname, label: "Meetup Group's URL", type: "text", placeholder: "Enter your Meetup group's url to retrieve your events", prepend: 'http://www.meetup.com/' %>
</div>
<div class="field">
<%= f.text_field :meetup_apikey, label: 'Meetup API Key', placeholder: 'Enter your Meetup API key (Required for advanced meetup functionality)' %>
</div>
<%= f.hidden_field :status %>
<%= f.hidden_field :audit_status %>
<%= f.hidden_field :sref %>
<div class="col-sm-10" style="padding-right: 26px;">
<div class="actions pull-right">
<%= f.submit 'Update Group', class: "action-btn btn-rounded btn-large" %>
</div>
</div>
<% end %>
Is there a way to have the optional form fields save as nil instead of empty by default? Currently have a hack in where if the form field returned empty..it would overwrite as nil.
Any feedback or insight would help. Thanks!
The strip_attributes gem can take care of this for you.
https://github.com/rmm5t/strip_attributes
Could you post more context, please?
With what we've got here, my ideas are:
"" comes from somewhere in the view (please paste it)
Perhaps you're calling somewhere to_s on the :meetup_urlname? nil.to_s will return "" in ruby.
Rails only checks for field confirmations if there is a corresponding _confirmation field in the request.
To explain it further, here is a sample Rails view:
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= f.label :email %>
<%= f.email_field :email, autofocus: true %>
<%= f.label :password %>
<%= f.password_field :password, autocomplete: "off" %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, autocomplete: "off" %>
<%= f.submit "Sign up" %>
<% end %>
This form works as expected.
However, if you remove:
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, autocomplete: "off" %>
Passwords are no longer confirmed.
The problem I am having is all my JSON requests are going through successfully without a corresponding _confirmation field.
This request is successful:
{
"email": "me#example.com",
"password" : "somepassword"
}
This is not:
{
"email": "me#example.com",
"password" : "somepassword",
"password_confirmation": "_somepassword
}
Also, for the second JSON, if password and password_confirmation match then the request is successful(which is understood).
I think the first request should not be successful since there is no password confirmation.
Is this a bug or an expected behaviour since confirmation fields should be tied to ActiveRecord and not HTML from helpers.
How will one replicate field confirmations in Rails based JSON APIs then?
I think that validation of the confirmation is kind of useless in the context of an JSON API. You would basically only validate that the developer read your API description.
Therefore I would argue that it makes sense that it does not validate if the confirmation key is missing. Since a form would always return both fields it would always validate user input.
If you really want to enforce the confirmation (even on the JSON API) just follow the docs:
NOTE: This check is performed only if password_confirmation is not nil. To require confirmation, make sure to add a presence check for the confirmation attribute:
validates_presence_of :password_confirmation, if: :password_changed?
I am trying to do something somewhat common - generate an array of unique items in a list on a form when using collection_select. A User has many payments, and a Payment belongs_to user. I am using Devise and so know the current_user.
I can successfully generate a list of emails with duplication in the view using the code:
<%= form_for(#payment) do |f| %>
.
.
.
<%= f.collection_select :email, current_user.payment, :email, :email, {:include_blank => "Please select"} %>
However, I have not been able to get the same list of emails eliminating duplicates. This question has been asked a few times on Stackoverflow, but I have not had success implementing the other solutions. I have tried (without luck):
<%= f.collection_select :email, current_user.payment.collect(&:email).uniq, :email, :email, {:include_blank => "Please select"} %>
<%= f.collection_select :email, current_user.payment.pluck(:email).uniq, :email, :email, {:include_blank => "Please select"} %>
I get the error message undefined method 'email' for "test#example.com":String. Can anyone help me understand (1) why the code I am using is incorrect, and (2) what change to make in the code?
Greatly appreciate any help you can provide!
To understand the error (question 1):
collection_select expects a collection of objects, and sends the value and label methods to these objects.
After collect or pluck, you have an array of strings, so sending the value or label method :mail to this string gives your error.
To solve this, refine the relation that is returned by current_user.payments:
<%= f.collection_select :email, current_user.payment.select(:email).uniq, :email, :email, {:include_blank => "Please select"} %>
uniq, as part of relations, only works with Rails 3.2 or later. With earlier Rails versions it is:
<%= f.collection_select :email, current_user.payment.select('distinct email'), :email, :email, {:include_blank => "Please select"} %>
I realize this question is dated, but if calling .uniq does not remove the duplication, calling .distinct can also provide a solution here:
<%= f.collection_select :email, current_user.payments.select(:email).distinct, :email, :email, {:include_blank => "Please select"} %>
I am using simple form in my Rails application, i tried to set value and class to a specific input field just like below
<%= f.input :email, :required => true, :autofocus => true, :value => "Email", :class => "myclass" %>
Html output :
but i could not able to see value and class set in the actual html form.
What am i doing wrong?
I'm not using the simple_form plugin for Rails, but as the documentation says you should use the :input_html attribute for this.
Do the following:
<%= f.input :email, required: true, autofocus: true, input_html: { :value => "Email", class: "myclass" } %>
How can I add placeholder text to my f.text_field fields so that the text comes pre-written by default, and when a user click inside the fields, the text goes away - allowing the user to type in the new text?
With rails >= 3.0, you can simply use the placeholder option.
f.text_field :attr, placeholder: "placeholder text"
In Rails 4(Using HAML):
=f.text_field :first_name, class: 'form-control', autofocus: true, placeholder: 'First Name'
For those using Rails(4.2) Internationalization (I18n):
Set the placeholder attribute to true:
f.text_field :attr, placeholder: true
and in your local file (ie. en.yml):
en:
helpers:
placeholder:
model_name:
attr: "some placeholder text"
I tried the solutions above and it looks like on rails 5.* the second agument by default is the value of the input form, what worked for me was:
text_field_tag :attr, "", placeholder: "placeholder text"
Here is a much cleaner syntax if using rails 4+
<%= f.text_field :attr, placeholder: "placeholder text" %>
So rails 4+ can now use this syntax instead of the hash syntax
In your view template, set a default value:
f.text_field :password, :value => "password"
In your Javascript (assuming jquery here):
$(document).ready(function() {
//add a handler to remove the text
});
This way works to me.
<%= form_for #product do |f| %>
<%= f.label :name %>
<%= f.text_field :name, placeholder: "Drill hammer" %>
<% end %>
As you can see, to implement a placeholder, you just can add the "placeholder: "text here", after your text_field name.
Hope my answer can be understood!