# "Widgets" on a web site - what's the best way to do it?

**URL:** <https://www.ruby-forum.com/t/widgets-on-a-web-site-whats-the-best-way-to-do-it/144877>\
**Category:** Rails\
**Created:** [August 3, 2008, 7:57pm UTC](https://www.ruby-forum.com/t/widgets-on-a-web-site-whats-the-best-way-to-do-it/144877 "2008-08-03T19:57:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hubert\_SSSSSSSSSpic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/73ab20/32.png) [@Hubert\_SSSSSSSSSpic](https://www.ruby-forum.com/u/Hubert_SSSSSSSSSpic)\
**Post date:** [August 3, 2008, 7:57pm UTC](https://www.ruby-forum.com/t/widgets-on-a-web-site-whats-the-best-way-to-do-it/144877/1 "2008-08-03T19:57:55Z")

</div>

Hi all,

I am having general question on how do you solve problem of rendering  
interactive (or not) “widgets” on your web sites - areas that user  
interact with and provide special behavior.

For example, one might want to put these in sidebar to forecast  
weather, or to manage nested resource’s attributes.

What I usually do, is call “render\_component” to render some other  
controller’s actions. But this has some nasty side-effects, like  
errors with “protect\_from\_forgery”.

I know that Components are in Rails no more, and my approach really  
not perfect.

Rendering only partials, without some business logick behind is not  
really the solution.

Could you please share how you resolve similiar issues?

Thanks,  
H.

---

<div class="post-metadata">

**Author:** ![Hubert\_SSSSSSSSSpic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/73ab20/32.png) [@Hubert\_SSSSSSSSSpic](https://www.ruby-forum.com/u/Hubert_SSSSSSSSSpic)\
**Post date:** [August 3, 2008, 10:46pm UTC](https://www.ruby-forum.com/t/widgets-on-a-web-site-whats-the-best-way-to-do-it/144877/2 "2008-08-03T22:46:56Z")

</div>

Hubert ÅÄ™picki wrote:

> Rendering only partials, without some business logick behind is not  
> really the solution.

Why not?

If I would want to do something like that, I think I’d first store the  
users preferences and configuration and layout in the database.

For each widget I would create a new widget class. On a users request  
I’d instanciate each widget the user uses and put into an array, or a  
hash. Maybe something like:  
{:leftside =\> [forecast\_widget, sudoku\_widget], :center =\> [joke\_widget]  
…}

Then i’d pass the hash to the view and render each widget with render  
:partial =\> the\_widget

---

<div class="post-metadata">

**Author:** ![Hubert\_SSSSSSSSSpic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/73ab20/32.png) [@Hubert\_SSSSSSSSSpic](https://www.ruby-forum.com/u/Hubert_SSSSSSSSSpic)\
**Post date:** [August 3, 2008, 11:09pm UTC](https://www.ruby-forum.com/t/widgets-on-a-web-site-whats-the-best-way-to-do-it/144877/3 "2008-08-03T23:09:13Z")

</div>

I also don’t think that having one controller for each widget would make  
for an elegant solution.

What about a generic “widget\_controller”, that passes the xhr request to  
the correct widget instance?

---

<div class="post-metadata">

**Author:** ![Hubert\_SSSSSSSSSpic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/73ab20/32.png) [@Hubert\_SSSSSSSSSpic](https://www.ruby-forum.com/u/Hubert_SSSSSSSSSpic)\
**Post date:** [August 3, 2008, 10:55pm UTC](https://www.ruby-forum.com/t/widgets-on-a-web-site-whats-the-best-way-to-do-it/144877/4 "2008-08-03T22:55:56Z")

</div>

Hi,

On Aug 3, 10:46 pm, Stefan B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Then i’d pass the hash to the view and render each widget with renderWEll ,  
> :partial =\> the\_widget

Well, yes…

But what about nested controllers, that I would like to display on the  
same site and interact with using AJAX calls?

For example, I want to have Todos controller “show” action to render  
TodoItems “index” action - and then, have all TodoItems actions called  
using AJAX requests. I really easily can do it using render\_component,  
but some people seem to be telling me it’s not the right way…

Best,  
H.

---

<div class="post-metadata">

**Author:** ![Hubert\_SSSSSSSSSpic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/73ab20/32.png) [@Hubert\_SSSSSSSSSpic](https://www.ruby-forum.com/u/Hubert_SSSSSSSSSpic)\
**Post date:** [August 4, 2008, 9:15am UTC](https://www.ruby-forum.com/t/widgets-on-a-web-site-whats-the-best-way-to-do-it/144877/5 "2008-08-04T09:15:41Z")

</div>

HI,

On 3 Sie, 23:09, Stefan B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I also don’t think that having one controller for each widget would make  
> for an elegant solution.
> 
> What about a generic “widget\_controller”, that passes the xhr request to  
> the correct widget instance?

Yes, I understand your point.

But my case is that I would love to have many different areas on my  
page, that use different controllers behind and operate smoothly with  
CSRF protection with protect\_from\_forgery, and Ajax calls.

render\_component and followed ajax request change hidden field value  
that’s used for protection from CSRF, and then my users get errors. I  
understand that one option is to disable protect\_from\_forgery… but  
is there other way to do it?

Best,  
H.

---

<div class="post-metadata">

**Author:** ![Hubert\_SSSSSSSSSpic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/73ab20/32.png) [@Hubert\_SSSSSSSSSpic](https://www.ruby-forum.com/u/Hubert_SSSSSSSSSpic)\
**Post date:** [August 4, 2008, 1:00pm UTC](https://www.ruby-forum.com/t/widgets-on-a-web-site-whats-the-best-way-to-do-it/144877/6 "2008-08-04T13:00:03Z")

</div>

Hubert ÅÄ™picki wrote:

> HI,

> render\_component and followed ajax request change hidden field value  
> that’s used for protection from CSRF, and then my users get errors. I  
> understand that one option is to disable protect\_from\_forgery… but  
> is there other way to do it?
> 
> Best,  
> H.

I can’t help you with render\_component, never used it. Sorry ☹
