我的设置:Rails 3.0.9,Ruby 1.9.2
routes.rb resources :tasks resources :projects do resources :tasks,:constraints => { :protocol => "http" } do put :cancel,:on => :member end end
耙路线显示
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"}
_form.html.erb for subscription <%= form_for [@project,:url => { :action => 'cancel',:method => "put" } do |f| %> <%= f.submit "Cancel your task"%> <% end %>
任何人都知道正确的form_format语法来完成这个?
解决方法
如果有人想要答案,这是我必须做的
<%= form_for [@project,:url => cancel_project_task_path(@project,@task) do |f| %>
我花了太长时间才能弄清楚这一点,希望这有助于下一个毫无戒心的开发人员.