LoginSignup
5
5

More than 5 years have passed since last update.

Sinatra routing with parameters

Posted at

Sinatraは偉大なので別にregexpで拾わなくてもparameters拾ってくれるし舐めてはいけない。

get %r{/data/(ID\d{6})\?type=(.+)} do |id, type|
  @id = id
  @type = type
  haml :coolestpageever
end

こんな風に書いて /data/ID000001?type=xml とかにアクセスしても無視されて404に飛ばされる。

sinatraは偉大であるのでこんな阿呆なことをする必要はない。?以降のパラメータはわざわざルーティングに書かなくてもparams[:sym]で拾ってくれる。

get %r{/data/(ID\d{6})} do |id|
  @id = id
  @type = params[:type]
  haml :coolestpageever
end

なんでも残らずregexpで拾わなくてもいいのである。Sinatraは偉大なのだ。

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5