redirect_to problem

Hi all,

I tried to post this yesterday, but it seems to have been lost in the void, so reposting.

I’m having a problem with redirect_to preserving the HTTP verb, in this case PUT, when redirecting.

I send an AJAX PUT request to to an action in TasksController, and want to redirect to the show action in the TaskListsController when processing is complete like the following:

redirect_to @tasklist, :notice => t(‘successful’)

From the docs I would assume this would sent a GET /task_lists/1, but instead is sending PUT /task_lists/1, causing the update action to be called…

Controllers:

class TasksController < ApplicationController

def move_task_down

def move_task_down

@task = Task.find(params[:id])

respond_to do |format|

if @task.move_lower

format.html { redirect_to @task.task_list, :notice => t(‘task_lists.successfully_moved’) }

else

format.html { redirect_to @task.task_list, :alert => t(‘task_lists.error_move_task’) } end

end

end

end

class TaskListsController < ApplicationController

def show

@task_list = TaskList.find(params[:id], :include => [:tasks])

respond_to |format|

format.html

end

end

def update

usual update stuff

end

end

Models:

class Task < ActiveRecord::Base

belongs_to :task_list

end

class TaskList < ActiveRecord::Base

has_many :tasks, :order => ‘position asc’, :dependent => :destroy

end

routes.rb:

put ‘tasks/:id/move_down’=> ‘tasks#move_task_down’, :as => ‘move_task_down’

resources :task_lists

development.log:

Started PUT “/task_lists/task/406137300/move_down?locale=ja” for 127.0.0.1 at 2012-03-21 09:40:16 +0900

Processing by TaskListsController#move_task_down as JS

Parameters: {“locale”=>“ja”, “id”=>“406137300”}

:

Redirected to http://localhost:3000/task_lists/1007688486?locale=ja

Completed 302 Found in 444ms

Started PUT “/task_lists/1007688486?locale=ja” for 127.0.0.1 at 2012-03-21 09:40:17 +0900

Processing by TaskListsController#update as JS

Parameters: {“locale”=>“ja”, “id”=>“1007688486”}

Why is the redirect_to using PUT and not GET?

Any advice would be greatly appreciated.

Thanks,

Martin

Hi all,

I tried to post this yesterday, but it seems to have been lost in the void, so reposting.

I'm having a problem with redirect_to preserving the HTTP verb, in this case PUT, when redirecting. I send an AJAX PUT request to to an action in TasksController, and want to redirect to the show action in the TaskListsController when processing is complete like the following:

redirect_to @tasklist, :notice => t('successful')

From the docs I would assume this would sent a GET /task_lists/1, but instead is sending PUT /task_lists/1, causing the update action to be called...

Controllers: class TasksController < ApplicationController   def move_task_down

  def move_task_down     @task = Task.find(params[:id])

    respond_to do |format|       if @task.move_lower         format.html { redirect_to @task.task_list, :notice => t('task_lists.successfully_moved') }       else         format.html { redirect_to @task.task_list, :alert => t('task_lists.error_move_task') }       end     end   end end

class TaskListsController < ApplicationController   def show     @task_list = TaskList.find(params[:id], :include => [:tasks])     respond_to |format|       format.html     end   end   def update     # usual update stuff   end end

Models: class Task < ActiveRecord::Base   belongs_to :task_list end

class TaskList < ActiveRecord::Base   has_many :tasks, :order => 'position asc', :dependent => :destroy end

routes.rb: put 'tasks/:id/move_down'=> 'tasks#move_task_down', :as => 'move_task_down' resources :task_lists

development.log: Started PUT "/task_lists/task/406137300/move_down?locale=ja" for 127.0.0.1 at 2012-03-21 09:40:16 +0900   Processing by TaskListsController#move_task_down as JS   Parameters: {"locale"=>"ja", "id"=>"406137300"}    : Redirected to http://localhost:3000/task_lists/1007688486?locale=ja Completed 302 Found in 444ms

Started PUT "/task_lists/1007688486?locale=ja" for 127.0.0.1 at 2012-03-21 09:40:17 +0900   Processing by TaskListsController#update as JS   Parameters: {"locale"=>"ja", "id"=>"1007688486"}

Why is the redirect_to using PUT and not GET? Any advice would be greatly appreciated.

Please paste in the output from rake routes here. I am guessing that the show of a task_list is somehow scoped to PUT, but I'd be hard-pressed to guess what that might mean. You might try disambiguating the issue in your redirect call by using the REST helper:

task_list_path(@task.task_list)

instead of redirecting to the bare object (which I agree, is supposed to work).

Walter