2013-02-17 39 views
0

這裏是我的問題:露營在問號分裂網址

Camping是問號分裂的URL。

因此,如果我們有一些像這樣的代碼:

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.火狐
3. Safari瀏覽器

+0

問號是表示一切URL特殊字符之前,它是路徑和每個之後的查詢字符串。這是你如何構建網址,或者你是否試圖訪問名稱中包含問號的資源?如果是後者,請嘗試使用編碼'%3F'替換問號進行編碼。 – 2013-02-17 03:10:21

+0

@Zach Kemp - 我可以構造沒有問號的網址嗎?例如。 example.com/text=foobarbazqux而不是example.com/?text=foobarbazqux – 2013-02-17 03:37:03

回答

1

路由只匹配URL的路徑

https://example.com/hello/world?a=this&b=hello&c=world#nice 
^  ^  ^  ^     ^
Schema Host  Path   Query parameters  Fragment 

在野營您可以通過@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 
+0

仍然進入索引,除非爲表單指定了操作並將操作路由到「搜索」控制器。不是一個大問題。 – 2013-02-19 00:23:21