Search code examples
rubymodel-view-controllerurlcampingmarkaby

Camping is splitting URLs at question marks


Here's my problem:

Camping is splitting urls at question marks.

So if we have some code like this:

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

Run camping path/to/file
After going to localhost:3301 in your browser and entering some text in the text field and hitting submit, you should see everything after the slash, but instead it splits the url at the question mark and because it thinks there is nothing after the slash, it takes you to the index.

Question: Is it possible to set up input so it does not use a question mark, or can I make camping not split at the question mark?

Appendix A
Tested in
1. Google Chrome
2. Firefox
3. Safari


Solution

  • The route only matches the path of the URL:

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

    In Camping you get access to the query parameters through @input:

    @input.a # => "this"
    @input.b # => "hello"
    @input.c # => "world"
    

    Query parameters are more like "options" that you can pass to the controller. For example, you don't want to have a separate controller to handle "sorting by name" and "sorting by date", so instead you use query parameters:

    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
    

    That way, all of these works:

    /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