DRY methodology

because I am a grasshopper…

Now that I can sort my ‘list’…is there a logical way of not repeating
myself to having essentially the same list view with multiple sorts?

i.e.

def list_cl

ordered by clients last name

@placement_pages, @placements = paginate(
  :placements,
  :include => [:client],
  :order_by => 'clients.last_name',
  :per_page => 14)

end

def list_fac

ordered by facility

@placement_pages, @placements = paginate(
  :placements,
  :include => [:facility],
  :order_by => 'facilities.name',
  :per_page => 14)

end

etc.

and then have links at the top of each column header to sort…

<%= link_to ‘Client’, :action => ‘list_cl’ %>
<%= link_to ‘Facility’, :action => ‘list_fac’ %>

Do I just have one ‘def list’ and then use something like a ‘case’
statement? If so, how do I tell it from the link_to which value?

Craig

Pass an extra parameter after the action.

<%= link_to ‘Client’, :action => ‘list’, :sort_by => ‘client’ %>

Estelle.

OK then :sort_by becomes sort of an instance variable then? It doesn’t
really create the ‘order_by’ on its own right?

Craig

It becomes available via param[:sort_by]


– Tom M.

Ok - I’m tugging at it…

<%= link_to ‘Placements’, :controller => ‘placements’, :action
=> ‘list’, :case_manager_id => case_manager %>

calls placements_controller.rb

this works…
def list
@placement_pages, @placements = paginate(
:placements,
:conditions => [“placements.case_manager_id = ?”, ‘15’],
:include => [:client],
:order_by => ‘clients.last_name’,
:per_page => 14)
end
or
:conditions => "placements.case_manager_id = “15”,
but
:conditions => "placements.case_manager_id =
params[:case_manager_id],

gives me the syntax error near : # the :conditions line… (whether I
use param or params - I don’t understand the difference between the two
terms).

and I can see from <% debug params %>
!map:HashWithIndifferentAccess
action: list
case_manager_id: “15”
controller: placements
{“action”=>“list”, “case_manager_id”=>“15”, “controller”=>“placements”}

that case_manager_id with value of 15 is coming through when I simply
comment out the conditions line altogether.

I’ve been playing with this but it’s the same form as the ‘:sort_by’
function so when I get one, I get both.

Try this:

:conditions => [“placements.case_manager_id = ?”,
params[:case_manager_id] ],

looks like this is going to have to wait until morning…I can’t seem to
stay connected to remote terminal now…somewhat spotty old dish
technology at clients until they move… ;-(

I will certainly try it…you have been spot on.

Thanks

Craig

*** bing ***

You are truly remarkable.