Reputation: 27
In my model i have :code attr_accessor
<%= text_field_tag :code, "", class: 'form-control text optional', placeholder: 'Code' %>
When the form is submitted, i get from params the :code, and i want so validate some errors
controller
@tourney_subscribe = TourneySubscribe.new(params.tourney_subscribe)
validate_code(params[:code]) if params[:code].present?
#now it adds the error
# when .valid? it clean the error
if @tourney_subscribe.valid?
...
else
render :next
end
but in my controller i'm using .valid? and it clean the errors and then don't show the :code error
In this case how to treat errors with an attr_accessor?
Upvotes: 0
Views: 55
Reputation: 15258
You can validate virtual attribute as usual model attribute, for example
class MyModel < ApplicationRecord
attr_accessor :code
validates :code, presence: true
end
And other validations too
Upvotes: 0