Kinda a noob rails programmer, but this would save me a ton of headache. Currently, I'm trying to add validation to my devise sign on page, such as only allowing the sign up to complete if their email ends with a certain extension. Does anyone know where the file location stands that overlooks the sign on page? I've looked over all models and views but can't seem to find it. Thank you!
I think that you can use validates_format_of in your user.rb model. You can use rubular.com to create the regex.
validates_format_of :email, :with => /MYREGEXHERE/
Simply add validation to your email field. There is nothing special you have to do. like:
validates :email, format: { with: /my_ending_string\z/, message: "must ends with my_ending_string" }
Related
I am seeing a lot of signups from these email ids and want to block them. I am using Devise + Rails 4.
In my user.rb, I am playing with
validates :email, presence: true, format: /\w+#\w+\.{1}[a-zA-Z]{2,}/
but I am not sure how to use format to block these domains. Any help?
Also is this the right way to do it or can we have a better approach to block some domains/free email providers via Devise?
Edit - I found http://guides.rubyonrails.org/active_record_validations.html, Section 2.5 format where I can use without: still, I am not sure how to block email signups from 163.com and mail.ru, can I use "validates:"twice as writing a single regexp to exclude both will be difficult?
just drop this in somewhere, you can leave your validations as-is.
class User < ActiveRecord::Base
EXCLUDE_DOMAINS = %w{163.com mail.ru}
before_validation do
domains = EXCLUDE_DOMAINS.join('|')
pattern = %r{##{domains}$}
if matched_domain = pattern.match(self.email)
self.errors.add(:email, "can't be #{matched_domain}")
end
end
I have a class Links. The class has these attributes
Twitter
Facebook
Pinterest
Quora
I want to write a validation that makes sure when a user inserts a link into a textbox it starts with http or https.
The validation works, but I have a an issue when the user does not insert a link at all. It still tries to run the validation on the empty attribute and raises the error. The below code is my attempt at checking for a empty attribute.
Here's my validation:
class Links < ActiveRecord::Base
attr_accessible :facebook, :twitter, :quora, :pinterest
validate :formatted_link
def formatted_link
links = %w(facebook twitter pinterest quora)
if links.any? {|link| self[link].nil?}
#Don't want it to do any validation if column is nil.
#Would like to drop column if user doesn't add a link.
else
validates_format_of links, :with => URI::regexp(%w(http https))
errors.add(:base, "Your link must start with http or https.")
end
end
Reason:
If a user just submits "www.twitter.com/username" the url get's appended to my sites url "mydomain.com/page/www.twitter.com/username. Not sure why this is happening. If there is something else I could do to prevent this or something I am missing please let me know.
Thanks.
I think you need to convert links to a symbol before passing it to validates_format_of, but since it seems to be working for you, I may be wrong or your sample code above may be missing that detail.
Either way, you can skip validation on blanks with:
validates_format_of :foo, with: some_format, allow_blank: true
I think you're trying to do conditional validation in a roundabout way. Since you really just want to allow blanks, you can do it this way:
class Link < ActiveRecord::Base
Networks = [:facebook, :twitter, :quora, :pinterest]
attr_accessible *Networks
validates_format_of *Networks, :with => URI::regexp(%w(http https)), :allow_blank => true
end
I am new to Rails. I am trying to create user management + login system using Devise. All is great except two things:
1) Is there any way to remove "email" field and/or validation when signing up? When remove :validatable from model, then passwords doesn't validate too.
2) What is best way to make user management in system? Something like overriding devise sign up?
I was searching in google for answers and readed documentation, but don't found answers.
Thank you.
1) You should remove :validatable and write your own validation in User model.
2) You can create users by hand in devise:
#user = User.new(:field1 => "something", :field2 => "something else", :password => "secret", :password_confirmation => "secret")
#user.save
if created object passes validation it will be created just like with devise signup action.
You can remove email validation with
def email_required?
false
end
in your User model
1) I don't know from which version onwards, but at least at that time when you where asking your question, there was already a tutorial at the devise github page how to do it.
They outline two ways, one was described here, how to manage that. And you can keep their validations.
2) As for CRUD users they also have a tutorial which is a bit more recent.
Hope this still helps someone ;-)
I have a user model that validates_uniqueness_of :login, :e-mail. When a user enters his information into user/new.html.erb, the create action may fail because of either of the two fields.
How can I customize my flash to be more helpful to the user, telling them which (or both) of the fields they need to change the next time around?
flash[:error] = #user.errors.full_messages.to_sentence
should do the job. But I would recommend that you display the error right next to the field which contains invalid data. Plugins like formtastic will do this for you automatically.
Check the API for more ideas.
Pretty new at all this. I have a simple form for users to enter a couple pieces of information and then input their email address and push the submit button. I want to make it mandatory that they have to fill out their email address in order to push the submit button. If they don't fill out their email address they should get an error message on the email box that says the email can't be blank. I know this is super simple but I need exact help on where to put what code. I've been researching this all night and know that part of the code should go in the application_controller and other should go in the html file where the actual text_field:email is.
I'd be grateful if someone could clearly tell me what the necessary steps are for doing this. Thanks!
It should go in your model. Add this:
class Model < ActiveRecord::Base
validates_presence_of :email
end
Check this link for more info: http://guides.rails.info/activerecord_validations_callbacks.html#validates-presence-of
In Rails 2, which I would assume you are using, validations go in the model. Which is located in $Rails_app_directory/app/model/$Classname.rb
In order to add ActiveRecord validations you can use the line
validates_presence_of :email_address
You should also consider using Rails to generate a confirmation field and filtering out ill-formatted email addresses. You could accomplish the former with:
validates_confirmation_of :email_address
with this, all you need to add to your form is a text_field for :email_address_confirmation
and the latter with a regular expression such as:
validates_format_of :email_address, :with => /\A[\w\.%\+\-]+#(?:[A-Z0-9\-]+\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|jobs|museum)\z/i
From snipplr, place in your model
validates_format_of :email,
:with => /^([^#\s]+)#((?:[-a-z0-9]+\.)+[a-z]{2,})$/i,
:message => 'email must be valid'