Gabriel,
> I've saved the data in one *unique* session object.. and this way I can
access it...
My solution too; it is called *params[:user]* .. in and out in *1 shot*..:-)
> about the crazy validation feedback code... sorry about that (but it
actualy worked!).
there was amusement but no irony in my comment; I really enjoyed the
irreverence of placing a javascript handler inside a render :text...
Raul
On Mon, Oct 27, 2008 at 7:00 PM, Gabriel <[EMAIL PROTECTED]> wrote:
> Hey all.
>
> Raul, thank you for the feedback.
> Your solution is simpler and cleaner than mine, and I guess it's enough to
> complete the task.
>
> I only disagree with saving data directly to session before creating the
> User object - User.new() - because the session can store and handle the
> entire object, with all of his attributes... I've saved the data in
> one unique session object - session[:user] = User.new(params[:user]) - and
> this way I can access his data just like we did with instance variables in
> previous lessons (session[:user].name, session[:user].age and
> session[:user].hobby). The address data is blank in session (since it exists
> in the AR model) and can be filled in the second step. Then comes the time
> to persist the object to database, performing the required validations.
>
> Oh, about the crazy intantanious validation feedback code with onclick
> handled links, I was just trying to "raise my standards" and solve some more
> complex challenges... sorry about that (but it actualy worked!).
>
> Thanks.
> Gabriel.
>
>
> 2008/10/27 raul parolari <[EMAIL PROTECTED]>
>
>> [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
-~----------~----~----~----~------~----~------~--~---