Forum: Ruby on Rails DRY methodology

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-02-08 02:35
(Received via mailing list)
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
0e5e8a4176061ad36e3e31311e28e309?d=identicon&s=25 Estelle Winterflood (Guest)
on 2006-02-08 05:35
(Received via mailing list)
Pass an extra parameter after the action.

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

Estelle.
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-02-08 06:20
(Received via mailing list)
OK then :sort_by becomes sort of an instance variable then? It doesn't
really create the 'order_by' on its own right?

Craig
59de94a56fd2c198f33d9515d1c05961?d=identicon&s=25 Tom Mornini (Guest)
on 2006-02-08 07:09
(Received via mailing list)
It becomes available via param[:sort_by]

--
-- Tom Mornini
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-02-08 07:24
(Received via mailing list)
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.
0e5e8a4176061ad36e3e31311e28e309?d=identicon&s=25 Estelle Winterflood (Guest)
on 2006-02-08 08:04
(Received via mailing list)
Try this:

:conditions => ["placements.case_manager_id = ?",
params[:case_manager_id] ],
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-02-08 08:19
(Received via mailing list)
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
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-02-08 14:44
(Received via mailing list)
*** bing ***

You are truly remarkable.
This topic is locked and can not be replied to.