Take the 2-minute tour ×

My setup: Rails 3.0.9, Ruby 1.9.2

I added a custom action to a nested resource task.

resources :tasks
resources :projects do
  resources :tasks, :constraints => { :protocol => "http" } do 
    put :cancel, :on => :member

rake routes shows

cancel_project_task PUT /projects/:task_id/tasks/:id/cancel(.:format) {:protocol=>"http", :action=>"cancel", :controller=>"tasks"}

In my controller,

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

    respond_to do |format|
      if @task.cancel
        format.html { redirect_to(@task, :notice => 'Task was successfully canceled.') }
        format.html { render :action => "edit" }

I need to define a form to perform the action, here's what I have currently

_form.html.erb for subscription
  <%= form_for [@project, @task], :url => { :action => 'cancel' } do |f| %>
    <%= f.submit "Cancel your task"%> 
  <% end %>

It throws an error

No route matches {:action=>"cancel", :controller=>"tasks"}

I also tried adding :method => "put" with the same error

_form.html.erb for subscription
  <%= form_for [@project, @task], :url => { :action => 'cancel', :method => "put" } do |f| %>
    <%= f.submit "Cancel your task"%> 
  <% end %>

Anyone knows the correct form_format syntax to accomplish this?

share|improve this question

1 Answer 1

up vote 18 down vote accepted

In case anyone wants the answer, here's what I had to do

<%= form_for [@project, @task], :url => cancel_project_task_path(@project, @task) do |f| %>

It took me way too long to figure this out, hopefully this helps the next unsuspecting developer.

share|improve this answer

Your Answer


