javacoder
javacoder

Reputation: 17

One Model and two controller validation Ruby on Rails?

One thing that confuses me the most is when doing validation in one model with two controllers. I have a login system which register and logs users in. There both use the same model but both does not use the same amount of HTML widgets. One controller contains password, retype password, user name, first & second name and so on. The second controller uses only the user name and password fields. How would you do validation in the same model for this situation?

Thank you

here is the controller that register new users:

class UsersController < ApplicationController
  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)

    if @user.save
      session[:user_id] = @user.id
      redirect_to '/cool'
    else
      @user = Newuser.new
      @user.valid? 
      @user.errors.messages
      render 'new'
    end
  end

  private

  def user_params
    params.require(:user).permit(:name, :email, :password, :salt, :firstname,      :secondname, :address, :postcode)
  end
end

second controller:

class LoginsController < ApplicationController
  before_filter :authorize

  def index
    @rentals = Rental.where(user_id: current_user.id).limit(5)
    @buys = Buy.where(user_id: current_user.id).limit(5)
    @users = User.where(id: current_user.id)
    @buyGames = BuyGame.where(user_id: current_user.id).limit(5)
  end 

  def destroy
    @user = User.find(params[:id])
    @user.destroy
    redirect_to '/logout'
  end

  def edit
    @user = User.find(params[:id])
  end

  def update
    @user = User.find(params[:id])

    if @user.update(account_params)
      redirect_to '/cool'
    else
      render 'edit'
    end
  end

  private

  def account_params
    params.require(:user).permit(:name, :email, :password, :salt, :firstname, :secondname, :address, :postcode)
  end
end

Here is my model:

class User < ApplicationRecord
  has_secure_password
end

Upvotes: 0

Views: 287

Answers (3)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230531

One way to go is to remove validations from the model and put them in form objects. For this case, you'll have two form objects, each with its own set of validations. And you use the appropriate one in respective controllers. Something along these lines:

# logins_controller
def update
  login_form = FormObjects::LoginForm.new(login_params)
  if login_form.valid?
    redirect_to '/cool'
  else
    render 'edit'
  end  
end

# users controller
def create
  signup_form = FormObjects::SignupForm.new(user_params)
  if signup_form.save
    redirect_to '/cool'
  else
    render 'new'
  end
end

# signup_form
module FormObjects
  class SignupForm
    include ::ActiveMode::Model

    validate_presense_of :email, :password, :password_confirmation, :address, :whatever_else

    def save
      # create user here
    end
  end
end

# login_form
module FormObjects
  class LoginForm
    include ::ActiveMode::Model

    validate_presense_of :email, :password
  end
end

Upvotes: 1

Tom Lord
Tom Lord

Reputation: 28305

I believe the trick you are looking for is to define validation actions on create/update of the model. Something roughly along these lines:

class User < ActiveRecord::Base
  # These are example validations only; replace with your actual rules.
  validates :password, confirmation: true
  validates_presence_of :username
  validates :first_name, presence: true, format: {with: /.../}, on: create
  validates :last_name, presence: true, format: {with: /.../}, on: create
end

...However, I am unclear why you would want to do this in your specific example. It would be advisable to always run all validation checks on fields like first_name, to help maintain data integrity.

Upvotes: 0

Sandeep Kapil
Sandeep Kapil

Reputation: 992

You can simply specify validations on actions, that is:

validates :first_name, presence: true, on: :create # which won't validate presence of first name on update or any other action

Upvotes: 0

Related Questions