使用友好URL链接到资源

时间:2012-05-13 09:13:49

标签: ruby-on-rails-3 routes

我正在尝试在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的新手,我还没有找到导致此错误的原因。谁能帮我弄清楚我做错了什么?

0 个答案:

没有答案