作为 Edx 的 SaaS 课程的一部分,我正在努力使用我的第一个 Rails 应用程序。卡住了一个大概相当基本的路由问题。
搜索、试验和错误只让我在下面进行了适度的尝试,所以我现在第一次尝试使用 stackoverflow。
任务是在单击标题时使“电影”表的“标题”列可排序。到目前为止,我已经添加了下面标记为粗体的 'link_to' .. 'movie_path...' 部分。
%h1 All Movies
%table#movies
%thead
%tr
%th= link_to "Movie Title", movie_path(:sort => "title")
%th Rating
%th Release Date
%th More Info
%tbody
...
我的 routes.rb 文件只有基于resources :movies
. 为了处理“排序”选项,我更改了 MoviesController 的基本“索引”方法,如下所示:
def index
sort = params[:sort]
@movies = Movie.find(:all, **:order => "title"**) **if :sort == "title"**
end
现在我收到此错误消息:
Started GET "/movies" for 127.0.0.1 at 2013-01-27 14:58:55 -0600
Processing by MoviesController#index as HTML
Rendered movies/index.html.haml within layouts/application (1.6ms)
Completed 500 Internal Server Error in 20ms
ActionView::Template::Error (No route matches {:action=>"show", :controller=>"movies"}):
4: %table#movies
5: %thead
6: %tr
7: %th#title_header= link_to "Movie Title", movie_path
8: %th Rating
9: %th Release Date
10: %th More Info
app/views/movies/index.html.haml:7:in `_app_views_movies_index_html_haml__237103960__639242128'
问题):
- 我应该定义一个单独的方法“排序”而不是在其中包含“排序”选项
index
吗? - 无论哪种情况,如何正确路由请求?
提前致谢!