我正在尝试在Rails 3中写博客,所以我有帖子。我想为这些帖子制作好的路线:posts/year/month/day/post-title
。所以我在model/post.rb
中覆盖了to_param并使用friendly_id
作为标题:
extend FriendlyId
friendly_id :title, :use => :slugged
def to_param
"#{year}/#{month}/#{day}/#{title.parameterize}"
end
我将此添加到routes.rb
:
resources :posts do
collection do
match ":year/:month/:day/:id", :action => "show"
end
member do
post 'comment'
delete 'comment/:comment_id', :action => 'destroy_comment', :as => 'destroy_comment'
end
end
在show
这有效:
<%= link_to image_tag("/images/edit_icon.png"),
{ :controller => 'posts', :action => 'edit' }, :id => @post %>
但在index
中,它说
routing error:
No route matches {:action=>"edit", :controller=>"posts"}.
我是Rails的新手,我还没有找到导致此错误的原因。谁能帮我弄清楚我做错了什么?