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.
Craig W. (Guest)
on 2006-02-08 03: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
Estelle W. (Guest)
on 2006-02-08 06:35
(Received via mailing list)
Pass an extra parameter after the action.

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

Estelle.
Craig W. (Guest)
on 2006-02-08 07: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
Tom M. (Guest)
on 2006-02-08 08:09
(Received via mailing list)
It becomes available via param[:sort_by]

--
-- Tom M.
Craig W. (Guest)
on 2006-02-08 08: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.
Estelle W. (Guest)
on 2006-02-08 09:04
(Received via mailing list)
Try this:

:conditions => ["placements.case_manager_id = ?",
params[:case_manager_id] ],
Craig W. (Guest)
on 2006-02-08 09: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
Craig W. (Guest)
on 2006-02-08 15:44
(Received via mailing list)
*** bing ***

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