# Another session question

**URL:** <https://www.ruby-forum.com/t/another-session-question/66608>\
**Category:** Rails\
**Created:** [December 15, 2005, 7:19pm UTC](https://www.ruby-forum.com/t/another-session-question/66608 "2005-12-15T19:19:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Scott\_Phelps](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/85e7bf/32.png) [@Scott\_Phelps](https://www.ruby-forum.com/u/Scott_Phelps)\
**Post date:** [December 15, 2005, 7:19pm UTC](https://www.ruby-forum.com/t/another-session-question/66608/1 "2005-12-15T19:19:16Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Scott\_Phelps](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/85e7bf/32.png) [@Scott\_Phelps](https://www.ruby-forum.com/u/Scott_Phelps)\
**Post date:** [December 15, 2005, 10:45pm UTC](https://www.ruby-forum.com/t/another-session-question/66608/2 "2005-12-15T22:45:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Scott\_Phelps](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/85e7bf/32.png) [@Scott\_Phelps](https://www.ruby-forum.com/u/Scott_Phelps)\
**Post date:** [December 16, 2005, 1:17pm UTC](https://www.ruby-forum.com/t/another-session-question/66608/3 "2005-12-16T13:17:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Scott\_Phelps](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/85e7bf/32.png) [@Scott\_Phelps](https://www.ruby-forum.com/u/Scott_Phelps)\
**Post date:** [December 15, 2005, 11:37pm UTC](https://www.ruby-forum.com/t/another-session-question/66608/4 "2005-12-15T23:37:06Z")

</div>

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!
