views:

36

answers:

1

i have a problem implementing a list action with its own route. here is what i have (just important parts) routes.rb

   map.resources :posts   
   map.login  "login" ,:controller => "user_sessions" ,:action => "new" 
   map.logout "logout" , :controller => "user_sessions" , :action => "destroy"
   map.sort "sort" , :controller => "posts" , :action => "sort"

posts_controller.rb

class PostsController < ApplicationController

 def index
  logger.info "index params:::"+params.inspect
   @posts = current_user.posts.paginate :page => params[:page], :order => 'created_at   DESC'
  end
 end

  def show
   @post = Post.find(params[:id])
  end

  def edit
   @post = Post.find(params[:id])
  end


  def sort
   logger.info "sort params:::"+params.inspect
   @posts=current_user.posts.paginate(:page=>params[:page],:order=> params[:sort].to_s )
  if request.xml_http_request?
   render :update do |page|
    page.replace_html :posts , :partial => 'post'    
   end
  end
 end


end

I get this error ActiveRecord::RecordNotFound in PostsController#show

Couldn't find Post with ID=sort.

I think its because the app thinks that when i type http://localhost:3000/posts/sort i want to show element with id=sort.how can i fix that?? thank you in advance

+1  A: 

Indeed, you are in a REST context. When accessing posts with the :get method, the default routing send the request to the show action, with the :id param set to sort. Try to define the sort action in your routes, something like:

map.resources :posts, :collection => {:sort => :get}

Set the verb according to your needs, :get seems right since you're just retrieving data.

Then, remove the line

map.sort "sort" , :controller => "posts" , :action => "sort"

You should now also have a nice url: sort_posts_url

Yannis