2. Routing - 2.3 Route Globbing

2.3 Route Globbing

# /items/list/base/books/fiction/dickens
get 'items/list/*specs', controller: 'items', action: 'list'

def list
  specs = params[:specs] # e.g, "base/books/fiction/dickens"
end

# http://localhost:3000/items/q/field1/value1/field2/value2/...
get 'items/q/*specs', controller: "items", action: "query"

def query
  @items = Item.where(Hash[params[:specs].split("/")]) 
  if @items.empty?
    flash[:error] = "Can't find items with those properties" 
  end
  render :index 
end

Hash converts a one-dimensional arrayof key/value pairs into a hash! Further proof that in-depth knowledge of Ruby is a prerequisitefor becoming an expert Rails developer.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 文/首丘 如果能使时光的声音有知有觉,是因为拥有和失去,悉数呈现。 曾经之于时光,渴望倒流,愿意合上眼...
    澜羽阅读 2,849评论 0 0
  • 我是歌里的南山之南,你消失在了北秋之北 天色阴暗是我的世界,春花漫烂是昨日的映衬 信封里红叶李的树叶是曾经你给我的...
    字丁一阅读 2,895评论 0 0
  • 我看着窗外想着心事想着你 想你时 时光转过一个世纪 想你 我忘掉了姓名 想你时 鲸波骇浪都是大海温柔的呼吸 而窗外下起雨
    填我十万八千梦阅读 1,586评论 0 7