我是RubyonRails的初学者,我花了最后一个小时尝试做以下事情:
我有一个RubyonRails应用程序——一个包含文章和类别的博客。
我想为这些帖子提供另一个网址(我想
http://localhost:3000/news
而不是
http://localhost:3000/posts
)首先,我尝试替换控制器和
Posts
到
News
但是我放弃了(因为讨厌的单数复数形式)。然后我换了我的
map.resources :posts
(案例1)至
map.resources :news, :controller => "posts" #case 2
或
map.resources :posts, :as => 'news' #case 3
在里面
routes.rb
正如我看到的
some websites
. 它也不起作用。
我该怎么做?
编辑:
产量
rake routes
是(仅第一行):
对于情况1和3:
posts GET /posts {:action=>"index", :controller=>"posts"}
formatted_posts GET /posts.:format {:action=>"index", :controller=>"posts"}
POST /posts {:action=>"create", :controller=>"posts"}
POST /posts.:format {:action=>"create", :controller=>"posts"}
new_post GET /posts/new {:action=>"new", :controller=>"posts"}
formatted_new_post GET /posts/new.:format {:action=>"new", :controller=>"posts"}
edit_post GET /posts/:id/edit {:action=>"edit", :controller=>"posts"}
formatted_edit_post GET /posts/:id/edit.:format {:action=>"edit", :controller=>"posts"}
post GET /posts/:id {:action=>"show", :controller=>"posts"}
formatted_post GET /posts/:id.:format {:action=>"show", :controller=>"posts"}
PUT /posts/:id {:action=>"update", :controller=>"posts"}
PUT /posts/:id.:format {:action=>"update", :controller=>"posts"}
DELETE /posts/:id {:action=>"destroy", :controller=>"posts"}
DELETE /posts/:id.:format {:action=>"destroy", :controller=>"posts"}
案例2的输出:
news_index GET /news {:action=>"index", :controller=>"posts"}
formatted_news_index GET /news.:format {:action=>"index", :controller=>"posts"}
POST /news {:action=>"create", :controller=>"posts"}
POST /news.:format {:action=>"create", :controller=>"posts"}
new_news GET /news/new {:action=>"new", :controller=>"posts"}
formatted_new_news GET /news/new.:format {:action=>"new", :controller=>"posts"}
edit_news GET /news/:id/edit {:action=>"edit", :controller=>"posts"}
formatted_edit_news GET /news/:id/edit.:format {:action=>"edit", :controller=>"posts"}
news GET /news/:id {:action=>"show", :controller=>"posts"}
formatted_news GET /news/:id.:format {:action=>"show", :controller=>"posts"}
PUT /news/:id {:action=>"update", :controller=>"posts"}
PUT /news/:id.:format {:action=>"update", :controller=>"posts"}
DELETE /news/:id {:action=>"destroy", :controller=>"posts"}
DELETE /news/:id.:format {:action=>"destroy", :controller=>"posts"}
在案例2中我有错误,因为在我的源代码中我没有
edit_news
例如,我有
<%= link_to 'Edit', edit_post_path(post) %>