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.

There was a problem

You must be a member of this account.

This project is archived and is in readonly mode.

[PATCH] Make redirect_to support blocks.

#5643

With the new respond_with method, you can't provide the :location option that relies on an invalid object (that will raise a router error).

class UsersController < ApplicationController
  def create
    @user = User.create(params[:user])
    respond_with @user, :location => edit_user_path(@user)
  end

  def edit
    @user = User.find(params[:id])
    respond_with @user
  end
end

A solution for this is passing a block that can be executed later on.

class UsersController < ApplicationController
  def create
    @user = User.create(params[:user])
    respond_with @user, :location => proc { edit_user_path(@user) }
  end

  def edit
    @user = User.find(params[:id])
    respond_with @user
  end
end

I'm attaching a patch that fixes this issue.

Reported by Nando Vieira · September 16th, 2010 @ 09:59 PM

State: resolved
Milestone: 3.1
Assigned to: José Valim José Valim
Importance: Low

Activity

  1. Nando Vieira
    Nando Vieira
    • Title changed from Make return_to support blocks. to [PATCH] Make return_to support blocks.
    • Tag changed from block, redirection, redirect_to to actioncontroller, block, patch, redirection, redirect_to

    September 16th, 2010 @ 10:02 PM

  2. Nando Vieira
    Nando Vieira
    • Title changed from [PATCH] Make return_to support blocks. to [PATCH] Make redirect_to support blocks.

    September 16th, 2010 @ 11:19 PM

  3. Nando Vieira
  4. Rohit Arondekar
    Rohit Arondekar
    • State changed from new to open
    • Importance changed from to Low

    Opening ticket to get more eyes on this.

    September 17th, 2010 @ 01:45 AM

  5. José Valim
    José Valim
    • Milestone set to 3.1

    Do we really need to use instance_eval?
    And Nando, could you please generate the patch using git format-patch? This way it will keep you as the author.

    September 18th, 2010 @ 07:27 PM

  6. Nando Vieira
    Nando Vieira

    Hey Rockstar! I thought of yielding the controller instance, but we often have private methods and instance variables, so instance_eval makes more sense, I think.

    The new patch is attached.

    September 19th, 2010 @ 12:52 AM

  7. Repository