ruby-on-rails – Rails 3 form_for用于自定义操作的嵌套路由

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – Rails 3 form_for用于自定义操作的嵌套路由前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的设置: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"}

我也尝试添加:method => “put”有相同的错误

_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| %>

我花了太长时间才能弄清楚这一点,希望这有助于下一个毫无戒心的开发人员.

原文链接:https://www.f2er.com/ruby/265856.html

猜你在找的Ruby相关文章