1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00

Merge pull request #970 from davydovanton/convert-symbols-to-strings-in-readme

Convert params hash symbols to strings - final
This commit is contained in:
Katrina Owen 2015-02-04 18:12:52 -08:00
commit cad128a4c2
3 changed files with 41 additions and 41 deletions

View file

@ -56,8 +56,8 @@ hash-ből érhetünk el:
```ruby ```ruby
get '/hello/:name' do get '/hello/:name' do
# illeszkedik a "GET /hello/foo" és a "GET /hello/bar" útvonalakra # illeszkedik a "GET /hello/foo" és a "GET /hello/bar" útvonalakra
# ekkor params[:name] értéke 'foo' vagy 'bar' lesz # ekkor params['name'] értéke 'foo' vagy 'bar' lesz
"Helló #{params[:name]}!" "Helló #{params['name']}!"
end end
``` ```
@ -71,17 +71,17 @@ is el tudod érni:
``` ```
Az útvonalmintákban szerepelhetnek joker paraméterek is, melyeket a Az útvonalmintákban szerepelhetnek joker paraméterek is, melyeket a
`params[:splat]` tömbön keresztül tudunk elérni. `params['splat']` tömbön keresztül tudunk elérni.
```ruby ```ruby
get '/say/*/to/*' do get '/say/*/to/*' do
# illeszkedik a /say/hello/to/world mintára # illeszkedik a /say/hello/to/world mintára
params[:splat] # => ["hello", "world"] params['splat'] # => ["hello", "world"]
end end
get '/download/*.*' do get '/download/*.*' do
# illeszkedik a /download/path/to/file.xml mintára # illeszkedik a /download/path/to/file.xml mintára
params[:splat] # => ["path/to/file", "xml"] params['splat'] # => ["path/to/file", "xml"]
end end
``` ```
@ -89,7 +89,7 @@ Reguláris kifejezéseket is felvehetünk az útvonalba:
```ruby ```ruby
get /^\/hello\/([\w]+)$/ do get /^\/hello\/([\w]+)$/ do
"Helló, #{params[:captures].first}!" "Helló, #{params['captures'].first}!"
end end
``` ```
@ -106,7 +106,7 @@ tervezhetők, így például az user agent karakterláncot alapul véve:
```ruby ```ruby
get '/foo', :agent => /Songbird (\d\.\d)[\d\/]*?/ do get '/foo', :agent => /Songbird (\d\.\d)[\d\/]*?/ do
"A Songbird #{params[:agent][0]} verzióját használod" "A Songbird #{params['agent'][0]} verzióját használod"
end end
get '/foo' do get '/foo' do
@ -244,7 +244,7 @@ változók közvetlenül elérhetőek lesznek a sablonokban:
```ruby ```ruby
get '/:id' do get '/:id' do
@foo = Foo.find(params[:id]) @foo = Foo.find(params['id'])
haml '%h1= @foo.name' haml '%h1= @foo.name'
end end
``` ```
@ -253,7 +253,7 @@ De megadhatod egy lokális változókat tartalmazó explicit hash-ben is:
```ruby ```ruby
get '/:id' do get '/:id' do
foo = Foo.find(params[:id]) foo = Foo.find(params['id'])
haml '%h1= foo.name', :locals => { :foo => foo } haml '%h1= foo.name', :locals => { :foo => foo }
end end
``` ```
@ -332,7 +332,7 @@ használni:
end end
get '/:name' do get '/:name' do
bar(params[:name]) bar(params['name'])
end end
``` ```
@ -351,7 +351,7 @@ az útvonalakban és a sablonokban is:
get '/foo/*' do get '/foo/*' do
@note #=> 'Szeva!' @note #=> 'Szeva!'
params[:splat] #=> 'bar/baz' params['splat'] #=> 'bar/baz'
end end
``` ```
@ -388,7 +388,7 @@ a `pass` függvényhívással:
```ruby ```ruby
get '/guess/:who' do get '/guess/:who' do
pass unless params[:who] == 'Frici' pass unless params['who'] == 'Frici'
"Elkaptál!" "Elkaptál!"
end end

View file

@ -1261,7 +1261,7 @@ before '/protected/*' do
end end
after '/create/:slug' do |slug| after '/create/:slug' do |slug|
session[:last_slug] = slug session['last_slug'] = slug
end end
``` ```
@ -1319,7 +1319,7 @@ helpers FooUtils, BarUtils
enable :sessions enable :sessions
get '/' do get '/' do
"value = " << session[:value].inspect "value = " << session['value'].inspect
end end
get '/:value' do get '/:value' do
@ -1338,7 +1338,7 @@ end
use Rack::Session::Pool, :expire_after => 2592000 use Rack::Session::Pool, :expire_after => 2592000
get '/' do get '/' do
"value = " << session[:value].inspect "value = " << session['value'].inspect
end end
get '/:value' do get '/:value' do
@ -1659,12 +1659,12 @@ redirect to('/bar?sum=42')
enable :sessions enable :sessions
get '/foo' do get '/foo' do
session[:secret] = 'foo' session['secret'] = 'foo'
redirect to('/bar') redirect to('/bar')
end end
get '/bar' do get '/bar' do
session[:secret] session['secret']
end end
``` ```

View file

@ -78,8 +78,8 @@ end
~~~~ ruby ~~~~ ruby
get '/hello/:name' do get '/hello/:name' do
# 匹配 "GET /hello/foo" 和 "GET /hello/bar" # 匹配 "GET /hello/foo" 和 "GET /hello/bar"
# params[:name] 的值是 'foo' 或者 'bar' # params['name'] 的值是 'foo' 或者 'bar'
"Hello #{params[:name]}!" "Hello #{params['name']}!"
end end
~~~~ ~~~~
@ -91,17 +91,17 @@ get '/hello/:name' do |n|
end end
~~~~ ~~~~
路由范式也可以包含通配符参数, 可以通过`params[:splat]`数组获得。 路由范式也可以包含通配符参数, 可以通过`params['splat']`数组获得。
~~~~ ruby ~~~~ ruby
get '/say/*/to/*' do get '/say/*/to/*' do
# 匹配 /say/hello/to/world # 匹配 /say/hello/to/world
params[:splat] # => ["hello", "world"] params['splat'] # => ["hello", "world"]
end end
get '/download/*.*' do get '/download/*.*' do
# 匹配 /download/path/to/file.xml # 匹配 /download/path/to/file.xml
params[:splat] # => ["path/to/file", "xml"] params['splat'] # => ["path/to/file", "xml"]
end end
~~~~ ~~~~
@ -109,7 +109,7 @@ end
~~~~ ruby ~~~~ ruby
get /^\/hello\/([\w]+)$/ do get /^\/hello\/([\w]+)$/ do
"Hello, #{params[:captures].first}!" "Hello, #{params['captures'].first}!"
end end
~~~~ ~~~~
@ -127,7 +127,7 @@ end
~~~~ ruby ~~~~ ruby
get '/foo', :agent => /Songbird (\d\.\d)[\d\/]*?/ do get '/foo', :agent => /Songbird (\d\.\d)[\d\/]*?/ do
"你正在使用Songbird版本是 #{params[:agent][0]}" "你正在使用Songbird版本是 #{params['agent'][0]}"
end end
get '/foo' do get '/foo' do
@ -759,7 +759,7 @@ end
~~~~ ruby ~~~~ ruby
get '/:id' do get '/:id' do
@foo = Foo.find(params[:id]) @foo = Foo.find(params['id'])
haml '%h1= @foo.name' haml '%h1= @foo.name'
end end
~~~~ ~~~~
@ -768,7 +768,7 @@ end
~~~~ ruby ~~~~ ruby
get '/:id' do get '/:id' do
foo = Foo.find(params[:id]) foo = Foo.find(params['id'])
haml '%h1= foo.name', :locals => { :foo => foo } haml '%h1= foo.name', :locals => { :foo => foo }
end end
~~~~ ~~~~
@ -871,7 +871,7 @@ end
get '/foo/*' do get '/foo/*' do
@note #=> 'Hi!' @note #=> 'Hi!'
params[:splat] #=> 'bar/baz' params['splat'] #=> 'bar/baz'
end end
~~~~ ~~~~
@ -896,7 +896,7 @@ before '/protected/*' do
end end
after '/create/:slug' do |slug| after '/create/:slug' do |slug|
session[:last_slug] = slug session['last_slug'] = slug
end end
~~~~ ~~~~
@ -924,7 +924,7 @@ helpers do
end end
get '/:name' do get '/:name' do
bar(params[:name]) bar(params['name'])
end end
~~~~ ~~~~
@ -937,11 +937,11 @@ Session被用来在请求之间保持状态。如果被激活每一个用户
enable :sessions enable :sessions
get '/' do get '/' do
"value = " << session[:value].inspect "value = " << session['value'].inspect
end end
get '/:value' do get '/:value' do
session[:value] = params[:value] session['value'] = params['value']
end end
~~~~ ~~~~
@ -954,11 +954,11 @@ end
use Rack::Session::Pool, :expire_after => 2592000 use Rack::Session::Pool, :expire_after => 2592000
get '/' do get '/' do
"value = " << session[:value].inspect "value = " << session['value'].inspect
end end
get '/:value' do get '/:value' do
session[:value] = params[:value] session['value'] = params['value']
end end
~~~~ ~~~~
@ -1000,7 +1000,7 @@ halt 402, {'Content-Type' => 'text/plain'}, 'revenge'
~~~~ ruby ~~~~ ruby
get '/guess/:who' do get '/guess/:who' do
pass unless params[:who] == 'Frank' pass unless params['who'] == 'Frank'
'You got me!' 'You got me!'
end end
@ -1146,12 +1146,12 @@ redirect to('/bar?sum=42')
enable :sessions enable :sessions
get '/foo' do get '/foo' do
session[:secret] = 'foo' session['secret'] = 'foo'
redirect to('/bar') redirect to('/bar')
end end
get '/bar' do get '/bar' do
session[:secret] session['secret']
end end
~~~~ ~~~~
@ -1192,7 +1192,7 @@ end
~~~~ ruby ~~~~ ruby
get '/article/:id' do get '/article/:id' do
@article = Article.find params[:id] @article = Article.find params['id']
last_modified @article.updated_at last_modified @article.updated_at
etag @article.sha1 etag @article.sha1
erb :article erb :article
@ -1874,8 +1874,8 @@ class LoginScreen < Sinatra::Base
get('/login') { haml :login } get('/login') { haml :login }
post('/login') do post('/login') do
if params[:name] = 'admin' and params[:password] = 'admin' if params['name'] = 'admin' and params['password'] = 'admin'
session['user_name'] = params[:name] session['user_name'] = params['name']
else else
redirect '/login' redirect '/login'
end end
@ -1954,8 +1954,8 @@ class MyApp < Sinatra::Base
# 针对 '/define_route/:name' 的请求变量域 # 针对 '/define_route/:name' 的请求变量域
@value = 42 @value = 42
settings.get("/#{params[:name]}") do settings.get("/#{params['name']}") do
# 针对 "/#{params[:name]}" 的请求变量域 # 针对 "/#{params['name']}" 的请求变量域
@value # => nil (并不是相同的请求) @value # => nil (并不是相同的请求)
end end