Rails 3 resource routing without an id

坚强是说给别人听的谎言 提交于 2019-11-30 01:54:16

You can use to_param to craft the rails uses

class Post < ActiveRecord::Base
  ...
  def to_param
    "#{year}/#{month}/#{day}/#{title.parameterize}"
  end
end

More info: http://api.rubyonrails.org/classes/ActiveRecord/Base.html#method-i-to_param and http://www.seoonrails.com/to_param-for-better-looking-urls.html

If you go this route, you'll want to create a permalink attribute, and use Post.find_by_permalink(params[:id]) rather than Post.find(params[:id])

you should use path like this

post_path(@post, :year => 2010, :month => 3, :day => 16)

or

post_path(@post, :year => @post.created_at.year, :month => @post.created_at.month, :day => @post.created_at.day)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!