Question is as above. I have a record that I need to validate before deciding which form to show. The record is created invalid, and we check its validity to see if the form the user is on is the initial registration form or a subsequent form.
My issue is when I call record.valid? the record gets various error messages added to it which are then incorrectly displayed on the registration form. It's not possible for me to simply clear the errors after validating as I need the errors present if the user enters invalid data.
Is there any way to call valid? or an equivalent that does not add errors to the instance?
Thanks in advance
You want to validates with a steps system ? I think you can look at with_options block.
example :
with_options condition do
# your validations or anything...
# validates :attribute
end
documentation :
https://apidock.com/rails/Object/with_options
With good conditions you will avoid adding errors on validations already "validated" and focus on the new ones.
I hope I understood your request correctly.
After calling valid? you can clear errors from object ie.
record.errors.clear
Related
I'm using state_machine for an online purchasing process in Ruby on Rails. One of the states consists of the user filling in a form with billing information. This form is required, so I'm validating it when transitioning to the next state like so:
state :confirm do
validates_presence_of :name, :email
end
Where the form is on state :info and the next one is :confirm.
This works fine: If any fields are missing state_machine will not transition to the next state.
However I can't find information on how to display an error notification when the validation returns false. I want to display a message when this validation fails, and redirects to the form state. Thanks in advance for your help!
(I assume you have solved this long ago, but...)
I think this answer has nothing to do with state_machine. This is the problem:
I want to display a message when this validation fails, and redirects to the form state.
If you redirect, the object is reloaded and all the unsaved attributes are lost, as are any validation errors.
Standard way to handle failed validation is to (re-)render the edit view, not redirect to the edit action. This retains the unsaved attributes and validation errors.
So I'm writing a Facebook clone for a school project using Rails and I need some way to keep track of which users are logged in. At the moment, I'm a bit time-pressed, so I decided just to update the User model every time they visit a page with a last_seen attribute.
Problem is, the user model requires revalidation to successfully update_attributes. So I'm wondering two things:
Is there a better way to do this that I'm missing?
If not (or if it would take too long) is there a way to bypass the validation?
to 1.: I cant give you an exact answer but I think itwould be better to deal with this problem using a javascript on the clientside with a timer that sends an ajax request all xxx secounds and an action that receives this requests and saves it in a seperate table associated with the User.
to 2.: Yes there are some ways to bypass validations The most pragmatic way is to bypass the :validate => false option when saving the object but then you can use update_attributes:
object.save(:validate => false)
So there is also the possibility to use conditional validations that are only used when a specific condition is complyed. There is a railscast about that => http://railscasts.com/episodes/41-conditional-validations .
I'm having some issues using rails validation on a form. What I'm doing is, in a form users are entering a URL for some of their favorite sites. When they submit, my create method uses nokogiri to open to URL and parse the header of the web page. The problem I'm running into is, if the user enters the form and it's blank, my create method tried to open nil, which causes is to error out. I tried to add validation to the method like so:
validates :url, :presence => true
If the user enters the form, this validation does not catch the empty text area, and fails as it tried to open the path before checking the validation. Is there a way in rails to specify I want to run the validation at the start of the create rather the end (which I'm assuming is happening)?
Why not call nokogiri in an after_validation or after_create callback? This would elegantly solve your problem.
I am trying to create similar functionality as this gem authlogix-rpx, which optimistically saves the session/user object even if some of the validated fields are missing.
http://github.com/tardate/authlogic_rpx/tree/master/lib/authlogic_rpx (line 167)
With this gem it's possible to create the record that does not meet validation criteria and later on call the registration_complete? method which return false if all the validations do not pass.
I am not sure how this save is taking place, in my gem (which is an add on to authlogic using oauth2) I have tried doing save(false), save_with_vaidation_false but nothign really works, the validations fail and the record get saved.
Any ideas?
Thanks
You should be able to run user.save(false) to save the user and then use user.errors.full_messages to access the validation error messages.
Or you could provide the :on parameter to your validations to restrict them to :create, :update or both (:save)
validates_presence_of :email, :on=>:update
I have an additional member method (and a corresponding route) called download, for which I'd like to validate the existence of a password field.
Putting :validates_presence_of :password in my download method, I get an error claiming it's an undefined method.
Is this possible?
No, you can add errors to the model but validations live in the model. Check out the errors methods and add_to_base if you need to add errors in the controller for whatever reason.