Widgets in Rails

I develop web pages in Ruby On Rails and I'm thinking about how I can separate the parts of the view from the main view. I know the partials can do it.

So, first we must create a helper for getting widget paths:


# app/helpers/widget_helper.rb
module WidgetHelper
  def widget_path(widget, global=false)
    path = if global
            "widgets/#{widget}"
           else
            "#{controller.controller_name}/widgets/#{widget}"
           end
    return path
  end

end

This code does a very simple thing: computes the relative path of widget (partial) what we want display but it doesn't display it, just returns a path in string.

We can make two type of widgets: global and local. I using 'global' widgets from more controller's view, or from application.html.erb and 'local' widgets used only from its related controller's view. But this code doesn't make difference between two type of widget, so you can do what you want but I recommend my way.

The using of this helper is very-very easy.
In case of global widgets, you must create a directory called app/views/widgets, and put your global widgets here prefixed by '_' (similar as other partials). So, if I want to make widget named 'loginbox', then it's placed in app/views/widgets/_loginbox.html.erb and including it by:


<%= render :partial => widget_path('loginbox', true) %>

Local widgets are quite different: they are hosted under app/views/#{controller.controller_name}/widgets folder. Their files must be prefixed with '_' too. Using of them:


<%= render :patrial => widget_path('toolbar') %>

Enjoy!

Hozzászólások

"I creating a web pages"... Huh? Nálam itt kezdődik a parse error. :)

Hat nem tudom, de vakuljak meg, ha a web page tobbesszama nem a web pages. Marpedig en nem egy, hanem tobb weblapnal szaladtam bele ebbe a problemamba. Viszont nem akarom meghatarozni, hany darab weblap eseterol van szo, igy egyszeru tobbesszamot hasznalok.
--


()=() Ki oda vagyik,
('Y') hol szall a galamb
C . C elszalasztja a
()_() kincset itt alant.

Weblapokat fejlesztek - I develop web pages. Valoban, egy 'a' felesleg van. De a tobbi szerintem jo. Am ha megsem, ideirhatnad a jo megoldast mindannyiun okulasa vegett.
Amugy meg, inkabb a temahoz kene hozzaszolni, magyar topikoknal se elfogadott a nyelvi kekeckedes. Mindenki ugy ir, ahogy akar.
--


()=() Ki oda vagyik,
('Y') hol szall a galamb
C . C elszalasztja a
()_() kincset itt alant.

Nezd, attol nem lesz jobb. Igy talan okulok a hibaimbol. Nyilvan nem vagyok perfekt angol, mivel az iskolai tudason felul tobbe-kevesbe autodidakta modon tanulok. Azonban az nem megoldas, hogy csak belekotunk az emberbe, mert azzal inkabb csak hatraltatsz, mint segitesz.
Es meg lehet erteni. Persze, akarni kell.
--


()=() Ki oda vagyik,
('Y') hol szall a galamb
C . C elszalasztja a
()_() kincset itt alant.

I develop [a web page/ web pages] in Ruby On Rails and I am [ thinking about / wondering / ...] how I could separate the parts of the view from the main view.
==
`Have some wine,' the March Hare said in an encouraging tone.
Alice looked all round the table, but there was nothing on it but tea.