0

I have created a validation on a table but want to exclude the validation on a specific column in my controller create method. How would I go about doing this?

def validate_cookie_brand
 render json: 422, unless valid_cookie_brand?
end

def valid_cookie_brand?
  CookieBrand.exists?(cookie_brand: cookie_create_request_params[:cookie_brand]))
end

1 Answer 1

1

It will not be the whole solution but few things which you should keep in mind

you should respond in your action whenever cookie brand is valid or not. secondly, probably you are trying to enforce 422 HTTP status, not 422 as the response body. thirdly, one should not separate unless condition from execution by ','

code with all cautions above fixed:

def validate_cookie_brand if valid_cookie_brand? render json: { message: 'valid cookie' } # status is by default 200 else render json: { message: 'invalid cookie brand' }, status: 422 end end

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.