【问题标题】:How to make beautifull url如何制作漂亮的网址
【发布时间】:2012-08-29 14:36:10
【问题描述】:

我有像http://example.com/posts/?tag=2这样的网址

我的routes.rb

resources :posts do
  get 'tag', :on => :collection
end

我需要像http://example.com/posts/tag/linux这样的链接

我的桌子是:

posts(id,title)
tags(id,name)
taggings(id, post_id, tag_id)

【问题讨论】:

标签: ruby-on-rails routes slug


【解决方案1】:

你可以做某事。喜欢

resources :posts do
  get 'tag/:name', on: :collection
end

在您的控制器中,您可以在 url 参数中找到带有名称的标签,并获取带有该标签的所有帖子。

Tag.where(name: params[:name]).posts

或者您在 Post 模型中实现一个函数,例如 find_by_tag(tag),它会为您执行此操作,因此您只需调用

Post.find_by_tag(params[:name])

在你的控制器动作中什么是更好的可读性。

【讨论】:

    猜你喜欢
    • 2014-09-11
    • 2016-12-24
    • 2016-01-05
    • 2013-05-20
    • 2015-10-17
    • 2014-04-22
    • 2012-03-07
    • 1970-01-01
    • 2017-04-06
    相关资源
    最近更新 更多