Creating nice urls from forms

1 view
Skip to first unread message

Bryan

unread,
Oct 25, 2009, 6:50:40 PM10/25/09
to pdx...@googlegroups.com
Hi,

I'm wondering what the best way is to go about getting nice looking urls after a form is submitted.  Something like this:


example.com/colors:red,green,blue;height:tall
or
example.com/colors/red,green,blue/height/tall would work, although I don't like the implication that colors are higher in the hierarchy than height.

instead of

example.com?colors[]=red&colors[]=green&color[]=blue&height=tall

Would I use JavaScript on the client side (and if so, does anyone have an example I can look at?) or would I generate the urls on the server side and redirect to the new url? Or something different?

Thanks,
Bryan

Chuck Vose

unread,
Oct 25, 2009, 7:58:58 PM10/25/09
to pdx...@googlegroups.com
Instead of just telling you how to do this with routing can you give
us an idea of what you're trying to do? There may be a better way that
feels more railsy.

-Chuck

Bryan

unread,
Oct 25, 2009, 8:11:32 PM10/25/09
to pdx...@googlegroups.com
Sure..

In particular I want to add a long-overdue form to pages like this in UrbanDrinks:
http://www.urbandrinks.com/happyhour/usa/or/portland/southeast/today
.. where the form would have options like starting hour, ending hour, and which specials the person wants to return (beer, cocktails, food, and/or wine).

Thanks,
Bryan

Sam Goldstein

unread,
Oct 25, 2009, 9:27:59 PM10/25/09
to pdx...@googlegroups.com
Bryan,

I recently did something similar.  We wanted to support urls like http://example.com/search/keyword1/keyword2/keyword3
but also wanted to be able to search by keyword using a form submission.

We ended up redirecting within the controller action to the canonical path if the search param ('q') was present.  This code might help.

# app/controllers/search_controller.rb
class SearchController < ApplicationController
  def index
    if params[:q]
      return redirect_to search_url(:keywords => keywords_from_params)
    end
    @models = Model.search(keywords_from_params, params)
  end

  private
  def keywords_from_params
    params[:keywords].blank? ? params[:q].to_s.split(/\s+/) : params[:keywords]
  end
end

# config/routes.rb
ActionController::Routing::Routes.draw do |map|
  map.root :controller => :search, :action => :index
  map.search 'search/*keywords', :controller => :search, :action => :index
end

--
~ Sam Goldstein

Chuck Vose

unread,
Oct 25, 2009, 11:23:28 PM10/25/09
to pdx...@googlegroups.com
The other thing I might recommend is that anything after the # can be
in any format you desire. I'm not sure where Rails is headed in this
regard but I know a lot of ajaxified apps are starting to keep all
parameters behind the # since that's all we can change through
javascript.

This seems like it would dovetail with your development pattern quite
nicely since your site is already fairly js heavy it would make sense
not to have to do a refresh each time you want to change some options,
right?

-Chuck

Bryan

unread,
Oct 26, 2009, 1:27:57 PM10/26/09
to pdx...@googlegroups.com
Thanks Chuck & Sam. I'll look into both approaches.

-Bryan
Reply all
Reply to author
Forward
0 new messages