[英]Rails 3 form_for nested routes for custom action
我的設置: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
rake routes
展示
cancel_project_task PUT /projects/:task_id/tasks/:id/cancel(.:format) {:protocol=>"http", :action=>"cancel", :controller=>"tasks"}
在我的 controller 中,
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 語法來完成這個?
如果有人想要答案,這就是我必須做的
<%= form_for [@project, @task], :url => cancel_project_task_path(@project, @task) do |f| %>
我花了很長時間才弄清楚這一點,希望這可以幫助下一個毫無戒心的開發人員。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.