SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: routes_doubt

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    routes_doubt

    hi. as an experiment i decide to: rake routes , considering my routes.rb file has only: map.resources users. well, we all know the output, but based on this output the last 3 mappings which are: show(get), update(put) and destroy(delete):

    user GET /users/:id {:action=>”show”, :controller=>”users”}
    PUT /users/:id {:action=>”update”, :controller=>”users”}
    DELETE /users/:id {:action=>”destroy”, :controller=>”users”}

    based on the output above i decided to write the equivalent routes in the routes.rb file in order to mimic what map.resources does. so we will have 7 routes. to simplify let’s just consider here these last 3 routes. first, based on the output above, my conclusion is that i could use 3 routes with the same name: map.user and differentiate then with the :method symbol, being get, put and delete for each one of them … so rails would know which one to use based on the :method

    map.user ‘users/:id’, … , :method => :get
    map.user ‘users/:id’, … , :method => ut
    map.user ‘users/:id’, … , :method => :delete

    well, that doesn’t work … if i click delete in the view then the show action will be triggered. my 2nd conclusion is that since i’m not using map.resources and instead i’m using named_routes, then the routing system will use the top-first rule

    my doubt is, how rails understand map.resources since the output from rake routes doesn’t imply us to do something like:

    map.user_show ‘users/:id’, … , :method => :get
    map.user_update ‘users/:id’, … , :method => ut
    map.user_delete ‘users/:id’, … , :method => :delete

    plus if i named_route like the sample above, i don’t need to specify which method to use, cause the routes are named. right?

    then my 3rd conclusion is that rails should understand:

    map.user ‘users/:id’, … , :method => :get
    map.user ‘users/:id’, … , :method => ut
    map.user ‘users/:id’, … , :method => :delete

    based on the :method differentiation. i also did: :requirements => {:method => :delete} in routes.rb or user_path(user) in my view, but no success.

    obs. my delete link_to code:
    link_to ‘delete’, user, :confirm => ‘Confirm delete?’, :method => :delete

  2. #2
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well, answering my own doubt, once one decide to comment:

    # map.resources users

    and then use named paths ... you will have to differentiate the names like:

    map.user_show ...
    map.user_update ...
    map.user_delete ...

    why? because paths like that and also with simple paths, rails will be reading and executing the routes.rb file with TOP_FIRST priority and that's the rule!

    in other words, the rake routes output, eg:

    user GET /users/:id {:action=>"show", :controller=>"users"}
    PUT /users/:id {:action=>"update", :controller=>"users"}
    DELETE /users/:id {:action=>"destroy", :controller=>"users"}

    that output does NOT imply you can use same name paths and differentiate then with different :methods!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •