← Back to Upcase

Naming of the controller


(charlieanna) #1

I had a method called find_email(email) inside my userscontroller which actually checked if the email has been taken already. I am using this find_email method for client side validation. Should I create a new controller for this? Like EmailsController or FindEmailsController because in this case I am using show verb to show if the email has been taken or not.

Should the route then be resources :find_email,only: :show?


(Lenart Rudel) #2

To me having methods like find_email in controller solution smells. How about doing (email) validation inside the model and creating a more general route to check validity of a model? Later you can easily add more validations and check using the same way. What I have in mind is something like this:

class UsersController < ApplicationController
  # ...
  def validate
    @user = User.new(user_params).valid?
    ...
  end

private

  def user_params
    params.require(:user).permit(:email)
  end

end

class User
  validate email: true # write your own EmailValidator 
end


# routes
resources :users do
  post :validate, on: :member
end

# view
link_to "Validate", validate_user_path(@user)

(charlieanna) #3

I am calling check_email for doing a client side validation instead of server side validation. I am calling check_email using an ajax call. Love your solution, I think I can use this way too with ajax by defining an email validator.


(Lenart Rudel) #4

My point was to show that you can (and probably should) handle validations inside the model class - as an instance of class method. I don’t think you need a new controller for this. An extra route on :users collection should do. Checking availability could also be done like this:

class User
  def self.available?(attributes)
    # you could parse the attributes hash and check
    # for multiple attributes at the same time
    ...
    where(attributes[:email]).exists?
  end

# routes
resources :users do
  get :available, on: :collection # => GET on /users/available
end

# controller
def available
   User.available? params[:email]
end

(Chad Pytel) #5

Hi @ankur_kothari, I actually disagree - I believe you should create a new controller for doing the validation check. Probably /app/controllers/users/email_validations_controller.

I prefer to avoid additional non-restful actions whenever possible, and in this case, the additional controller makes sense to me.

I hope that helps,
-Chad


(charlieanna) #6

Yes @cpyel I have created a new controller called check_emails_controller.rb which checks to see if there is an email already present. And one called check_names_controller.rb. But I can use email_validations_controller to add in more checks on email like format.

But I see you have created a controller inside users directory. Any reason for that?

Thanks


(Chad Pytel) #7

I created it there just for organizational purposes, it’s not required and is more a matter of preference.

A note on your naming, controllers names should be made up of nouns, not verbs. So they name you’ve chosen is a verb, and I prefer my naming, or something similar, which would be a noun (resource)