开发者

Problem with routing in Rails

My routes.rb files has sty like this:


    match 'tickets/:conditions'           => 'tickets#index', :as => :list_tickets, :constraints => {:conditions => /(\w+):(\w+)\/?/}
    match 'tickets/sort::sort'            => 'tickets#index', :as => :root_sort
    match 'tickets/mailbox开发者_如何学Python::mailbox'      => 'tickets#index', :as => :view_mailbox
    match 'tickets/status::status'        => 'tickets#index', :as => :view_status  
    match 'tickets/tag::tag'              => 'tickets#index', :via => :get, :as => :show_tag

I want to get it working with addresses like: http://localhost:3000/tickets/mailbox:1/sort:date and inverted too: http://localhost:3000/tickets/sort:date/mailbox:1

What am I doing wrong?

Thanks!


I am sorry that I cannot give you a detailed and tested answer (lack of time, at the moment), but I suppose you may find Rails Routing from the Outside In guide helpful, especially the section Route globbing.

If you write (beware! I haven't tested this code):

match 'tickets/*conditions' => 'tickets#index'

then in your action you may parse the params[:conditions] and apply necessary filters. Something like:

params[:conditions].split('/').map {|c| c.split(':',2) }
# => [ ['sort','date'], ['mailbox','1'] ]

I'm afraid you will not have the named routes for all the possible conditions, but I hope it is not very important. I am sure you will handle the problem how to create the path with given conditions - just remember about calling CGI::escape on the necessary parts.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜