How can I check certain fields with rails designed for registration only

I have a set of custom fields attached to a development model called Entrant.

I have two forms, one for the registration form (three fields) and one that is in the account area (12 fields). Most of the custom fields area is required, but only in the form located in the account area.

How can I achieve this?

I am using rails 4.2 and ruby ​​2.1

+3


source to share


7 replies


You can simply specify checks for actions, that is:

validates :name, presence: true, on: :create # which won't validate presence of name on update action

      



If you ask where to place your own fields, then generate views and update the appropriate fields.

+4


source


There are several ways! You can do conditional checks like

class Entrant < ActiveRecord::Base
  validate :foo, if: :account_area?

  def account_area?
    !new_record? # Assumes that Entrant that has already been saved 
                 # is in the account area
  end
end

      

However, it looks like your needs are quite advanced and you should consider creating a form object

A form object is an object that takes parameters, performs validation against that data, and then stores an instance of the model.



class AccountForm
  include ActiveModel::Model
  include Virtus # Provides AR like attribute functionality and mass assignment

  def initialize(entrant)
    @entrant = entrant
  end

  attribute :foo, String
  validates :foo, presence: true # This is only used on the account page, so no need to mess with conditional logic

  def save
    if valid?
      persist!
      true
    else
      false
    end
  end

  def persist!
    @entrant.update_attributes(foo: self.foo)
  end
end

      

This is just a great example of how non-rails object-oriented programming can make your life easier and your application more convenient. Make the class as above, paste it into your application / forms and restart the server. Then in your controller you just pass the model to it

class EntrantController < ApplicationController
  def update
    @form = Form.new(Entrant.find(params[:id]))
    @form.attributes = params[:entrant]
    if @form.save
      redirect_to some_path
    else
      render "edit"
    end
  end 
end

      

+3


source


By default, only a request for an email / password combination is developed, you can add other fields by adding a sanitizer (see there -> Can you think of how to add an extra field to create a User Form? ). If you want to add other files for validation, you must create an additional Entrant controller and add a specific callback to your model. Usually:

after_update :validate_entrant_form, if: :property_changed?

      

Hope this helps you.

+2


source


validates :name, presence: true, if: :condition_holds?

def condition_holds?
 # some code here that evaluates to a boolean
end

      

+2


source


Perhaps this method will help you.

Add attribute to devise model

: say attr_accessor :validate_certain

. In your controller action, instantiate the model, let's say @user

should update like this @user.validate_certain = true

. and change the relevant validation conditions in model design

validates :name, presence: true, if: :validate_certain_changed?

def validate_certain_changed?
  validate_certain.present?
end

      

+1


source


When I need to do something like this I like to think of it as it checks if something is in the field, but you can also take nil

Entrant.validates_presence_of(:foo, :allow_nil => true) 

      

0


source


I also have this issue where you are using a client application with forms on separate pages, updating different client field settings.

I believe most of the solutions work, but I was looking for the simplest, simplest and most reliable way to implement the solution

It happened.

validates :phone, :country, :postal_code, :street_address, presence: true, allow_nil: true

      

allow_nil: true instructs the model to validate fields ONLY if it exists in the submitted form. If you need extra protection, you can use additional pairs such as : on =>: update

0


source







All Articles