2011-08-12 77 views
9

我的設置:Rails的3.0.9,1.9.2紅寶石Rails 3中的form_for嵌套路線自定義操作

我添加了一個自定義操作,以嵌套資源的任務。

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

rake routes顯示

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

在我的控制器,

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

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

我需要定義一個表格要執行的操作,這裏就是我目前

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

它引發了一個錯誤或

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

我也嘗試添加:method => "put"具有相同的錯誤

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

任何人都知道正確的語法form_format做到這一點?

回答

22

如果有人想要的答案,這是我必須做的

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

我花了太多的時間來弄清楚這一點,希望這有助於在未來不知情的開發商。

+0

謝謝,我浪費了數小時試圖使用'url:{action:my_action}' – constantine1