# Any way to pass state to a component?

**URL:** <https://www.ruby-forum.com/t/any-way-to-pass-state-to-a-component/72939>\
**Category:** Rails\
**Created:** [February 24, 2006, 6:51pm UTC](https://www.ruby-forum.com/t/any-way-to-pass-state-to-a-component/72939 "2006-02-24T18:51:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Homer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/df788c/32.png) [@Homer](https://www.ruby-forum.com/u/Homer)\
**Post date:** [February 24, 2006, 6:51pm UTC](https://www.ruby-forum.com/t/any-way-to-pass-state-to-a-component/72939/1 "2006-02-24T18:51:13Z")

</div>

I am trying set up a rails app that utilizes components. I need to be  
able to pass some state (a bunch of name value pairs) from the Main  
controller to the component.

Is there any way to pass this state info using render\_component?

Regards

---

<div class="post-metadata">

**Author:** ![Homer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/df788c/32.png) [@Homer](https://www.ruby-forum.com/u/Homer)\
**Post date:** [February 24, 2006, 7:01pm UTC](https://www.ruby-forum.com/t/any-way-to-pass-state-to-a-component/72939/2 "2006-02-24T19:01:53Z")

</div>

> I am trying set up a rails app that utilizes components. I need to be  
> able to pass some state (a bunch of name value pairs) from the Main  
> controller to the component.
> 
> Is there any way to pass this state info using render\_component?

Before you venture down the road of components, you might heed this  
disclaimer added to the documentation for components in Edge Rails:

When to use components

Components should be used with care. They’re significantly slower than  
simply splitting reusable parts into partials and conceptually more  
complicated. Don’t use components as a way of separating concerns  
inside a single application. Instead, reserve components to those rare  
cases where you truly have reusable view and controller elements that  
can be employed across many applications at once.

## So to repeat: Components are a special-purpose approach that can often be replaced with better use of partials and filters.

David Heinemeier H.  
[http://www.loudthinking.com](http://www.loudthinking.com) – Broadcasting Brain  
[http://www.basecamphq.com](http://www.basecamphq.com) – Online project management  
[http://www.backpackit.com](http://www.backpackit.com) – Personal information manager  
[http://www.rubyonrails.com](http://www.rubyonrails.com) – Web-application framework

---

<div class="post-metadata">

**Author:** ![Homer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/df788c/32.png) [@Homer](https://www.ruby-forum.com/u/Homer)\
**Post date:** [February 24, 2006, 7:53pm UTC](https://www.ruby-forum.com/t/any-way-to-pass-state-to-a-component/72939/3 "2006-02-24T19:53:32Z")

</div>

What are your thoughts on including ViewHelpers/ActionControllers in  
plugins  
versus components? When the need arises to do this, is that the time to  
make the switch from plugin to engine?

On the off chance you are okay with plugins including ActionControllers,  
is  
there a “safe” way to use named routes in the plugin init.rb to prevent  
naming conflicts with apps that make use of the plugin? For example I  
add a  
named route for FooController, with the name “stevelongdo\_foo”, more  
than  
likely I will not be causing a naming conflict with an existing app,  
unless  
someone else has my domain name…

---

<div class="post-metadata">

**Author:** ![Homer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/df788c/32.png) [@Homer](https://www.ruby-forum.com/u/Homer)\
**Post date:** [February 27, 2006, 1:59am UTC](https://www.ruby-forum.com/t/any-way-to-pass-state-to-a-component/72939/4 "2006-02-27T01:59:31Z")

</div>

There is. I’m using render\_component\_as\_string to call the component  
from within another controller, and I pass through the parameters there.  
My line of source for this looks like this.

@section\_content = render\_component\_as\_string(:controller =\>  
component\_controller, :action =\> action, :params =\>  
current\_component\_params)

Please feel free to drop me a line if you’d like to see more of this  
source code or talk about this further. I’m not 100% happy with my  
approach, and I’d like to hear more ideas. 🙂

regards,

Craig

---

<div class="post-metadata">

**Author:** ![Homer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/df788c/32.png) [@Homer](https://www.ruby-forum.com/u/Homer)\
**Post date:** [March 30, 2006, 6:31am UTC](https://www.ruby-forum.com/t/any-way-to-pass-state-to-a-component/72939/5 "2006-03-30T06:31:07Z")

</div>

Hello,

Still farily new to RoR and I’m running into a routing problem.

I am making an a multi-store ecommerce solution to replace one that I  
created a few years ago in ColdFusion, and I’m currently working on  
the administration site for it. What needs to happen is that a user  
first selects which store they want to administer, and then within  
that they can manage products, orders, etc. I really like/need to  
replicate the style of routing that basecamp employs for its projects  
(since its basically the same sort of deal).

Right now, I have:

map.store ‘store/:storeid’, :controller =\> ‘stores/main’, :action =\>  
‘setStoreSession’

This works fine. It takes in the storeid, then sets that id into a  
session variable and shows the user the main index page for that  
particular store. What I am not clear on is how to best continue  
this for other controllers and actions within that store.

What I want is to end up with something that will look like

localhost/store/54/products  
localhost/store/54/orders/detail/834  
etc

Any help on how to achieve that, and how to be able to automatically  
extract the session id into the appropriate place (rather not  
manually specify this every time…maybe a helper would be best for  
this? any way i could subclass the named mapping, so that store\_url  
always set the id if it was present in the session?).

Thanks,  
Dave
