Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Missing support for actions on a new resource in new routing DSL

#4328

The deprecated mapper supported adding resource actions that operated on a new resource, e.g:

  # /posts/new/preview
  map.resources :posts, :new => { :preview => :post }

The attached patch adds support for this to the new routing DSL. e.g:

  resources :posts do
    new do
      post :preview
    end
  end

To add this behaviour to singleton resources I've had to make a breaking DSL change. Rather than directly specifying member actions inside the resource block, they need to be nested inside a member block. e.g:

  resource :account do
    get :info # Now raises an exception
  end

  resource :account do
    member do
      get :info
    end

    new do
      post :preview
    end
  end

I did look at still allowing member routes on singleton resources to be specified directly inside the resource block but I personally felt it was better to have a consistent DSL.

Reported by Andrew White · April 5th, 2010 @ 07:09 AM

State: resolved
Milestone: 3.0.2
Assigned to: José Valim José Valim
Importance: Medium

Activity

  1. José Valim
    José Valim
    • Milestone cleared.
    • State changed from new to open
    • Assigned user changed from josh to José Valim

    I put it under my radar. If we are going to do it, it should be before the RC. Your patch also ensures the following syntax work?

    get :action, :on => :new
    

    June 7th, 2010 @ 10:11 AM

  2. Andrew White
    Andrew White

    I thought it did but there's an issue with the name prefix. I'll fix it and post a new patch later today.

    June 7th, 2010 @ 11:29 AM

  3. DHH
    DHH

    Not a fan of the required member block for singletons. It's fine to have new be in a block without having members do.

    June 7th, 2010 @ 11:10 PM

  4. José Valim
    José Valim

    Andrew, could you then update the patch based on DHH's feedback? Thanks!

    June 7th, 2010 @ 11:13 PM

  5. José Valim
    José Valim
    • Milestone cleared.

    Since we are not deprecating the API, it is not a blocker for RC. That said, I'm changing the milestone.

    June 7th, 2010 @ 11:32 PM

  6. Andrew White
    Andrew White

    Okay here's the updated patch. The member block for singletons is now optional, :on => :new works and tests pass.

    June 8th, 2010 @ 06:07 AM

  7. Repository
  8. Niels Meersschaert
    Niels Meersschaert
    • Importance changed from to Medium

    This commit doesn't appear to be in 3.0 final release. Is it slated to be released at some time? I am running into problems as a result of the broken new resource support without this patch.

    October 8th, 2010 @ 09:19 PM

  9. Andrew White
    Andrew White

    The mapper was refactored heavily between this commit and the final release but actions on a new resource are definitely in there - can you post a sample route where you think it is failing.

    October 8th, 2010 @ 09:30 PM

  10. Niels Meersschaert
    Niels Meersschaert

    Here is a sample route we are attempting to use in Rails 3.0
    resources :accounts do
    collection do

    get :outlook
    post :invoice_all
    

    end member do

    post :invoice
    get :manage_invoices
    post :regenerate_invoice
    

    end new do

    post :load_highrise_details
    post :get_highrise_contacts
    post :search_highrise_companies
    

    end end

    Running rake routes, these properly show the routes as expected. However, when actually trying to run those calls, it shows in the logfile as parameters {id => "new"} for any of our new resource routes. We have other similar routes where we support the same action both for new & members & then check if id param was passed to determine if we do Object.find(id) or Object.new. That worked perfectly in Rails 2.x, but has been a problem in our Rails 3 migration. We've resorted to using named routes for each new resource call to workaround this behavior, but obviously, it isn't an ideal solution.

    October 9th, 2010 @ 12:10 AM

  11. Niels Meersschaert
    Niels Meersschaert

    Here's the route snippet as an attached file due to formatting fun.

    Additionally due to the id => new, our current code gets a RecordNotFound due to running Object.find("new")

    October 9th, 2010 @ 12:14 AM

  12. Andrew White
    Andrew White

    You need to move your new actions ahead of your member actions - the routes are being matched in the order in which they are declared.

    October 9th, 2010 @ 06:00 AM

  13. Jeremy Kemper