Edit Devise email format - ruby-on-rails

I am currently using devise for user registration and will like to restrict signups based on domains like #company.com or #work.com. The aim is for users to only provide the first part of their email address for example "lucy.dale" then select their domain from a drop-down menu such as #company.com. Please see the code I have generated so far..
<div><%= f.label :email %><br />
<%= f.text_field :first_email, :autofocus => true %><%= f.select :domain, options_for_select([["Select One", ""], "#company.com", "#work.com", "#office.com"]) %>
As i don't have access to the Devise controller or mailers, please can you advise me on how I can join the :first_email & :domain attributes to give (lucy.dale#company.com) before passing it to the default :email attribute in Devise.
Thanks for your help in advance

I hope this help someone out there. Instead of extending the devise registration controller, i decided to use a Regex to validate the domains. This will allow me restrict the domains as shown below
validates_format_of :email, :with => /\A([^#\s]+)#(company\.com)|(work\.com)\z/
The regex was added to the model for validation.

Related

Validating JQuery autocomplete using client_side_validations in ruby on rails

I jave job which belongs_to client. I want to validate presence of client_id in jobs/new form. I am using jQueryUI autocomplete for picking client and I use client_side_validations for validations. After selecting client, I have javascript code to set value to client_id, but it is hidden_field which is not included in client side validations. Is there any way to validate its presence without modifying rails.validations.js?
Specifying :validate => true upon the entire form will generally do the trick. However, you can validate specific fields by targeting them directly with:
<%= f.text_field :name, :validate => { :presence => true } %>
I found trick for client side validation with rails auto complete
let auto complete field for company name
<% f.text_field :company_name %>
<%= f.autocomplete_field :company_name, ...%>
this will work without any problem

Validate field is unique compared to another field in same form

Say I have two fields in a new or edit form:
<%= f.text_field :email %>
<%= f.text_field :parent_email %>
How, in my model, can I validate that parent_email is different from email? The exclusion option seems like it might work, but I can't figure out how to access the email field's value within the model. Do I need to implement this in the controller instead?
validates :parent_email, exclusion: self.email # doesn't work, nor does :email
The following should work (but I guess there are cooler solutions out there):
class User
validate :email_differs_from_parent_email
private
def email_differs_from_parent_email
if email == parent_email
errors.add(:parent_email, "parent_email must differ from email")
end
end
end

Rails Model: How to make an attribute protected once it's created?

I was just wondering: How can i make an attribute "write once" ?
To be more precise, I'm using Devise and I want the user to be able to register with this email but then, once it's done, I want this email locked.
I read that forms can easily be bypass, so I want to make sure my model does that.
Also, i'm using in one of my form that: <%= f.email_field :email, :id => "email", :disabled => "disabled" %>
Is there any risks that an user can modify his email after being registered?
Thanks for your answers!
attr_readonly allows setting a value at creation time, then prevents modifying it on updates.
class MyModel < ActiveRecord::Base
attr_readonly :email
end

Customize devise sign_up

I'm new to the rails world so this can be a stupid question, but I cannot find the answer...
when I access a link like that:
www.domain.com/users/sign_up?email=some#email.com
I would like to have my "e-mail" field filled in with this parameter(some#email.com, in this case)
Any ideas of how can I achieve this (using devise) ?
Thanks!
EDIT
The answer:
<%= text_field :email, :value => params[:email] %>
You must use the name of the email field which is user[email] so www.domain.com/users/sign_up?user[email]=some#email.com I hope this helps.

How to validate user input in RoR?

I know that the RoR can do the validation in the models. But I want to confirm the user's password in the views. I means, it show two textfield for user to type password twice to ensure the user type the password correct, but in the database I only store one value. How can I handle it in the RoR?
In your model do:
validates_confirmation_of :password
In your view do:
<%= form.password_field :password %>
<%= form.password_field :password_confirmation %>
This is using the built in rails confirmation validation. It's will add the virtual accessor for you.

Resources