Hi all,
well, just after Pietro, some other people commented (privately) that the
validation should be done also at the first submit; they mentioned sites
where you enter some partial information, and you are brought to another
page (based on the info you submitted), where you add some more.
So, I surrender; I have been convinced! :-)
But they also think that it should be done by ActiveRecord, as it is absurd
(as I was saying) to do the validations in 2 places (one in AR, the other by
hand; imagine if validations requirements change and one needs to update 2
places).
So, the challenge is: validate also at the first submit, but using
ActiveRecord. How to do it?
I suggest this (I will not write the complete code, just to let you
experiment a bit with those methods): at the first submit, build a new user
with the params received, and find out if there are errors on just the
parameters you are interested on. The methods to be used are:
u = User.new(...)
u.valid?
u.errors.on xyz
Now, we have the best of both worlds: you validate the first screen, but you
let AR do it. And you proceed to the second form only if the first passes.
Perhaps this also answers Pietro's final question... if so, tanto meglio ("all
for the better" in italian).
Raul
On Tue, Oct 28, 2008 at 4:39 PM, raul parolari <[EMAIL PROTECTED]>wrote:
>
> Hi, Pietro
> taking ad litteram the words of the homework, you are right; one should
> verify that the values are there at the first submit. This can be done
> easily, by just verifying that each param is not empty/nil; so you can add
> that. I find it a bit dubious, as it just would do what ActiveRecord will do
> later. But it complies with the words of the exercise, and it is easy to do;
> so, go ahead.
>
> In any case, I don't understand the final comment when you say that you
> are stuck. Let me know what the problem is, so that I can help
>
> Raul
>
>
> On Tue, Oct 28, 2008 at 3:16 PM, Pietro Maggi <[EMAIL PROTECTED]>wrote:
>
>> Hi Raul,
>> just my two cents on this
>> I don't think that your clever solution comply with the homework
>> requirements that states: "When a user enters values into the three
>> fields in the index.html.erb and submits it, display another page
>> called address.html.erb that..."
>>
>> As I understand your description, you suggest to delay the validation
>> when saving the complete object, but the requirements for the homework
>> request that the second page is displayed only if the three fields
>> have values.
>>
>> So, it seems to me that the validation for the first three fields have
>> to be called on object creation (using parameter :on=>:create) instead
>> that on object saving (that is the default).
>> Any comment on this?
>>
>> BTW: at this moment I'm stuck here and it does not work :-/
>>
>> Best regards
>> Pietro
>>
>> On Mon, Oct 27, 2008 at 7:27 PM, raul parolari <[EMAIL PROTECTED]>
>> wrote:
>> > [I had again forgotten to do reply all, but the thread is here below for
>> > anyone interested]
>> >
>> > Gabriel,
>> >
>> > what you did is ingenious but a bit intricate. Consider this
>> alternative:
>> >
>> > a) when the user submits the 1st form, just save the data in session (by
>> the
>> > way, I wouldn't save the 'User.new()' in the session; I'd simply save
>> the
>> > data). No validation is needed here (else you end up doing all or part
>> of
>> > the job that AR will do later, as you in fact did).
>> >
>> > b) when the user submits the 2nd form, then create the user (with the
>> > session data, and the new param info). AR will do the full validation.
>> > Add perhaps this optional step:
>> > b1) if creation of the user is succesful, redirect to a 'show' form with
>> a
>> > message that user creation was done (and showing the user created).
>> > b2) if unsuccesful, redirect to 'index' (as this, in spite of its name,
>> is
>> > the page with a new form) with a message that the user creation was
>> > unsuccesful.
>> >
>> > This is simpler, and it tests all the tutorial points: session, flash,
>> > forms, render, redirect.
>> >
>> > In any case, it was certainly interesting to read your code (that
>> onclick
>> > handler to go back was cute); javascript knowledge is always welcome..
>> >
>> > Raul
>> >
>> >
>> > On Mon, Oct 27, 2008 at 10:40 AM, Gabriel <[EMAIL PROTECTED]> wrote:
>> >>
>> >> Dear Raul,
>> >> I believe that the problem is not that simple, and I think I've figured
>> >> out what was going on:
>> >> The model validations are called when the object tries to persist (when
>> >> you call "@user.save" as you wrote), but the homework requires that the
>> >> object must be persisted after the user inserts the address, his data
>> must
>> >> be addressed to the session for now. And also I think that the
>> objective of
>> >> this homework is to explore the characteristics of the
>> ApplicationController
>> >> class...
>> >> so here we go - i did this:
>> >> The model class remains like this (even only persisting the object
>> after
>> >> all the validations, I think this would help in session data loss
>> cases):
>> >> # models/user.rb
>> >> class User < ActiveRecord::Base
>> >> validates_presence_of :name, :age, :hobby, :address
>> >> end
>> >> # ----------
>> >> Then changed the "index" view form action to this new action in the
>> >> hello_controller.rb
>> >> # controllers/hello_controller.rb
>> >> ...
>> >> def first_step
>> >> text = "" # Var to recieve validation msgs.
>> >> params[:user].each do |param|
>> >> if param[1].length == 0 # Param value is empty
>> >> text += "#{param[0].capitalize} cannot be empty!<br />"
>> >> end
>> >> end
>> >> if text.length > 0
>> >> render :text => text + '<a href="#"
>> >> onclick="history.go(-1)">Back</a>' # Render a "validation failed" page
>> with
>> >> back button
>> >> else
>> >> session[:user] = User.new(params[:user]) # Save User object data
>> to
>> >> session (as required)
>> >> redirect_to :action => :add_address # Redirect to new page
>> >> end
>> >> end
>> >> ...
>> >> # ----------
>> >> Then I've created the "AddAddressToUser" migration and the
>> >> "add_address.html.erb" view, which shows the session data (just to be
>> sure
>> >> that the object data is persisted correctly to the session) and the
>> Address
>> >> field.
>> >> This field is contained whithin a form which calls the "save_user"
>> action
>> >> that I've created just like the "first_step", which performs another
>> >> validation to the Address field (including a "validation failed" page)
>> and,
>> >> finally persists the object if every field is filled.
>> >> I will not post every code here to save some space, but the concept is
>> the
>> >> same for the two steps.
>> >> Thanks for your help,
>> >> Gabriel.
>> >> 2008/10/27 raul parolari <[EMAIL PROTECTED]>
>> >>>
>> >>> Gabriel
>> >>>
>> >>> > @user = User.new(params[:user]) # <= This must create a new instance
>> >>>
>> >>> after this do:
>> >>> @user.save
>> >>>
>> >>> and see if this helps
>> >>>
>> >>> Raul
>> >>>
>> >>>
>> >>> On Mon, Oct 27, 2008 at 8:29 AM, Gabriel <[EMAIL PROTECTED]>
>> wrote:
>> >>>>
>> >>>> I'm stuck in the first step... - Add "cannot be blank" validation to
>> >>>> the three fields...
>> >>>>
>> >>>> Model validations aren't working at all... i've tried
>> >>>> "validates_presence_of", "validate_on_create"... Aren't these methods
>> >>>> called when I create a new instance of the User object?
>> >>>>
>> >>>> the 'respond' action (controllers/hello_controller.rb) does this:
>> >>>>
>> >>>> def respond
>> >>>> @user = User.new(params[:user]) # <= This must create a new instance
>> >>>> of the "User" object.
>> >>>> ...
>> >>>> end
>> >>>>
>> >>>> and the User (models/user.rb) is:
>> >>>>
>> >>>> class User < ActiveRecord::Base
>> >>>> # Nothing works here! "validates_presence_of" or "def
>> >>>> validate_on_create"... nothing!
>> >>>> end
>> >>>>
>> >>>> ----------
>> >>>>
>> >>>> Any clues?
>> >>>>
>> >>>> Thanks in advance,
>> >>>> Gabriel.
>> >>>>
>> >>>
>> >>
>> >
>> >
>> > >> >
>> >
>>
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "ruby-on-rails-programming-with-passion" group.
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/ruby-on-rails-programming-with-passion?hl=en?hl=en
-~----------~----~----~----~------~----~------~--~---