2011-08-12 57 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