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

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.

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

Hi,

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 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.

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?

HI,

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.

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 :frowning: