Another session question

I want to give a user a form to create a new Product, but instead of
persisting the product to the database I want to store it in a
session first for category assignment, etc… (in other controllers)

So when the user describes the product on the form I have a “Save and
Continue” button pointing to the action add_to_product_factory:

def new_product
@product = get_product
end

def add_to_product_factory
@product = Product.find(params[:product])

Will the code below grab the

original Product Object

@product = get_product
@product.add_product(product)

Here I want to determine the action based on what

tab the user clicked on to continue the process

redirect_to(:action => ???)
end

private
def get_product
session[:product_in_progress] ||= Product.new
end

Thanks in advance!

Hi Scott,

I am not really sure what you are asking. Are you asking how to
redirect to another controller? Or are you asking the best way to
capture the tab the user pressed and then forward to some controller?
If you are looking for something like this, I would say the tab
information could be sent up as a param, such as params[:tab], and you
could redirect to various controllers based on the value sent up using
something like:

redirect_to(:controller => ‘controller_a’, :action => ‘action_a’) if
params[:tab] == ‘a’
redirect_to(:controller => ‘controller_b’, :action => ‘action_b’) if
params[:tab] == ‘b’

However, another option may be to store this logic in your product
model, so that no matter where you store your products from, you could
assign the category within the model.

Hope that helps.

Tom

Hi Scott,

Perhaps you can just store the individual params in the session such as:
session[:new_contractor]
session[:new_billing]
session[:new_engineer]

And then when all of your information is collected, you could have
your project model be responsible for creating its dependencies, such
as:

Project.create(session[:new_contractor], session[:new_billing],
session[:new_engineer])

The Project model would then create each of the individual
dependencies. You could then wrap them all in a transaction so that
if one fails, they all will. You could also perform any business
logic related to storing these objects withing that method.

Tom

On Dec 15, 2005, at 4:41 PM, Tom D. wrote:

However, another option may be to store this logic in your product
model, so that no matter where you store your products from, you could
assign the category within the model.

That sounds more ideal. Let me be more specific…
Note I am switching from Product to Project now

I have a models set up like this:

class Project < ActiveRecord::Base
has_one :contractor_item, :dependent => true
has_one :billing_item, :dependent => true
has_one :engineer_item, :dependent => true
end
class ContractorItem < ActiveRecord::Base
belongs_to :project
end
class BillingItem < ActiveRecord::Base
belongs_to :project
end
class EngineerItem < ActiveRecord::Base
belongs_to :project
end

Now, to create a new Project I have these views:

new_contractor_item
new_billing_item
new_engineer_item

These views are set up as CSS tabs that the user can click
between and modify before clicking a “Create Project” link.

The goal is to collect all of the form data in a session and have
that form data repopulated during the Project creation until the user
clicks “Create Project” upon which the session will be reset and the
Objects will get saved to the database (after validation - of course)

I would love to do this all in the Project model, but I could not
find how to add these multiple objects to the same session within one
Project Model.

TIA!