这是控制器中的方法:
def sort
case params[:order_param]
when "title"
@cars = Movie.find(:all, :order => 'title')
when "rating"
@cars = Movie.find(:all, :order => 'rating')
else "release"
@cars = Movie.find(:all, :order => 'release_date')
end
redirect_to cars_path
end
这是视图:
%th= link_to "Car Title", :action => 'sort', :order_param => 'title'
%th= link_to "Rating", :action => 'sort', :order_param => 'rating'
%th= link_to "Release Date", :action => 'sort', :order_param => 'release'
如果我打开索引页面,则会出现以下错误消息:
No route matches {:action=>"sort", :order_param=>"title", :controller=>"cars"}
“rake routes”命令的结果
cars GET /cars(.:format) {:action=>"index", :controller=>"cars"}
POST /cars(.:format) {:action=>"create", :controller=>"cars"}
new_car GET /cars/new(.:format) {:action=>"new", :controller=>"cars"}
edit_car GET /cars/:id/edit(.:format) {:action=>"edit", :controller=>"cars"}
car GET /cars/:id(.:format) {:action=>"show", :controller=>"cars"}
PUT /cars/:id(.:format) {:action=>"update", :controller=>"cars"}
DELETE /cars/:id(.:format) {:action=>"destroy", :controller=>"cars"}