内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用
这是我的问题:
露营是在问号处拆分网址。
所以如果我们有这样的代码:
Camping.goes :CodeLine
module CodeLine::Controllers
class Index
def get
render :index
end
end
class TextEntered < R '/(.*)'
def get(textStringEntered)
"#{textStringEntered}"
end
end
end
module CodeLine::Views
def index
html do
head do
title "Uh Oh"
end
body do
text "Looks like you got to the index"
br
br
form :name => "input" do
input :type => "text", :name => "text"
input :type => "submit", :value => "Submit"
end
end
end
end
end
运行camping path/to/file
在进入localhost:3301
浏览器并在文本字段中输入一些文本并点击提交后,您应该看到斜杠之后的所有内容,但是它会将问题标记的URL拆分,因为它认为斜杠之后没有任何内容,所以需要你到索引。
问题:是否可以进行设置input
以使其不使用问号,或者我可以在问号处进行露营吗?
附录A 测试 1.谷歌浏览器 2. Firefox 3. Safari
该路由仅匹配URL 的路径:
https://example.com/hello/world?a=this&b=hello&c=world#nice
^ ^ ^ ^ ^
Schema Host Path Query parameters Fragment
在Camping中,您可以通过@input
以下方式访问查询参数:
@input.a # => "this"
@input.b # => "hello"
@input.c # => "world"
查询参数更像是可以传递给控制器的“选项”。例如,您不希望有一个单独的控制器来处理“按名称排序”和“按日期排序”,因此您使用查询参数:
class Search
def get
query = @input.q || "*"
page = (@input.page || 1).to_i
sort = @input.sort || "name"
@results = fetch_results_from_database_or_something(query, page, sort)
render :search
end
end
这样,所有这些工作:
/search?query=hello # Page 1, sort by name
/search?page=5 # Page 5, sort by name, search for everything
/search?query=cars&page=4&sort=date