Ruby on Rails 3 model validation


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
20 views
Suppose we have User model and it has first_name, last_name, email fields. All they required. Accordingly in the model we prescribe
something like this

validates :first_name, :presence => true
validates :last_name, :presence => true
validates :email, :presence => true

and it would be good if form was on one page.
Now the question is, how postupci with validation if the form is for update of first_name and last_name into one page and email to another.
Default to say so on the form where first_name and last_name if it sasamat there will be an error saying that email is required and this is true (I have the same email on another page need to be updated), and the same email on the form where the validation is to ask for first_name and last_name. I'm interested in Rails 3 to correctly handle this situation.

For example, in ASP.NET MVC I would like child ViewModel directly to a specific View to check the fields and then doing the mapping of properties from the parent model. How to do it in Rails 3?
by | 20 views

5 Answers

0 like 0 dislike
If I understand correctly your algorithm, first you have created a record with first_name and last_name, and then already in an existing record updates a field of the email.
Then try
validates :first_name, :presence => true
validates :last_name, :presence => true
validates :email, :presence => true,: on => :update
And when you add the email field to upload an existing object.
Then on the first form you will have to be verified the first two fields, you save the object.
Then, in the second form, you load object with already populated fields first_name and last_name, and then update it. And when you update work the validation on email field.
by
0 like 0 dislike
A bit outdated, but allows us to understand the principle railscast on this topic: railscasts.com/episodes/217-multistep-forms
by
0 like 0 dislike
I did call transition method and validators depending on the state
\r
In the example in github.com directly shows how to do this
\r
state :first_gear, :do second_gear
validates_presence_of :seatbelt_on
end
by
0 like 0 dislike
means clean Rails on a production system
\r
with_options :if => "cause == 1" do |client|
client.validates :prepay, :cost, :birthday, :phone1, :contract_date, :vin, :adress, :presence = > true
client.validates_presence_of :id_series, :id_number, :id_dep
client.validates_format_of :id_series, :id_number, :with => /[\\d\\s]/
client.validates_presence_of :fio
client.validates :car, :presence => true
end
\r
with_options :if => 'cause == 5' do |client|
client.validates_presence_of :fio, :manager, :trade_in_price, :trade_in_desc
client.validates_associated :used_car
\r
end
by
0 like 0 dislike
Conditions is evil. The model from this incredibly grows. Much easier to make a new level of abstraction in the application and call it, for example, types. So, we create two types of user:
UserRegisterType < User and UserEmailSetterType < User(name pridemate themselves). Now, in the context of these legacy models can be put down we need attr_accessible, validates and other stuff. Learn more about this solution, and the pitfalls you can read in the blog Cyril Makevnina
by
110,608 questions
257,186 answers
0 comments
36,770 users