Created
February 9, 2011 22:29
-
-
Save tonsV2/819459 to your computer and use it in GitHub Desktop.
Nested resources and pagination
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
route: | |
resources :muscle_groups do | |
resources :muscles | |
end | |
controller index method: | |
def index | |
if params[:muscle_group_id] | |
muscle_group_id = params[:muscle_group_id] | |
@muscles = Muscle.paginate :page => params[:page], :order => 'created_at DESC', :conditions => ['muscle_group_id = ?', muscle_group_id] | |
else | |
@muscles = Muscle.paginate :page => params[:page], :order => 'created_at DESC' | |
end | |
respond_to do |format| | |
format.html # index.html.erb | |
format.xml { render :xml => @muscles } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An else part is needed plus the use of like is overkill for an id column.
def self.search(search, page)
if !search.blank?
paginate :page => page, :conditions => ['muscle_group_id = ?', search], :order => 'name'
else
paginate :page => page, :order => 'name'
end
end