Problem with redirect_to and URL structure since upgrade to rails 2.0

Hi,

I have written a littel rails application with rails 1.x , and used something like

redirect_to :controller => "aaa" , :action => "bbb" , :id => 123

within a controller to jump into a different controller's action. It worked.

Since upgrade to Rails 2.0 it does not work anymore, but generates an error message like

Couldn't find SomeModelClass with ID=bbb

As far as I can see the URL structure has changed from rails 1.3 to 2.0

it was /controller/action/id but now is /controller/id/action

where e.g. /controller is mapped to 'index' and /controller/ id is mapped to 'show'

Does anybody know why this was changed and how to use redirect_to right now?

regards

You could use restful routing.

http://frozenplague.net/2008/01/06/restful-routing-an-overview/

Well, I had a view on that restful routing thing and found that I can't use it, it breaks some of the functionality.

I had a special action in my rails application that gets the caller's :controller, :action, and :id to enable returning to the page where it came from. This was easy since redirect_to would take variables as parameters.

I do not see how this would work with restful routing, since it defines fixed macros for the pathes.

Who decided to use restful routing with rails?

Hadmut wrote:

You could use restful routing.

Well, I had a view on that restful routing thing and found that I can't use it, it breaks some of the functionality.

I had a special action in my rails application that gets the caller's :controller, :action, and :id to enable returning to the page where it came from. This was easy since redirect_to would take variables as parameters.

I do not see how this would work with restful routing, since it defines fixed macros for the pathes.

Who decided to use restful routing with rails?

To your first question, you should have a route in your routes.rb that looks like:

  map.connect ':controller/:action/:id'

That is how you want it to stay compatible with your old rails app. Restful routes look more like:

  ':resource_name/:id/:optional_action'

Where optional action is not a basic CRUD operation (new, edit, promote, rate, etc.) So make sure your defined routes are the right format.

Now to keep track of where a user was, you dont need to controller/action/id. You can capture the URI of the request. In your controller, you can do

  def save_location     session[:return_to] = request.request_uri     #=> "/some/path_to/something/123"   end

  def restore_location     redirect_to session[:return_to]   end

So rather than controllers and actions, you simply capture the string url path, and it works great with redirect_to.