我正在构建一个简单的任务管理应用程序,当我尝试处理复选框以指示任务已完成时,会发生一个奇怪的问题。
这是我想出的代码:
<%= check_box_tag "id", "id", task.done,
:onclick => remote_function(
:update => "task",
:url => { :action => :update, :controller => :tasks, :id => task.id },
:with => "'task[done]=true'",
:complete => "alert('hi')" ) %>
它会打印复选框,并且会根据 task.done 的状态自动检查它。但是当我触发 onclick 并观察日志时,我看到以下条目:
于 2011-04-25 23:15:44 -0300 开始对 127.0.0.1 发布“/tasks/20” ActionController::RoutingError(没有路由匹配“/tasks/20”):
查看我的 config/routes.rb 文件,我有:
resources :tasks
你能帮我找出我做错了什么吗?为什么找不到路线?
以下是 _task.html.erb View 的完整代码。
<tr>
<td class="task">
<span class="tasktitle">
<%= check_box_tag "id", "id", task.done,
:onclick => remote_function(
:update => "task",
:url => { :action => :update, :controller => :tasks, :id => task.id },
:with => "task[done]=true",
:complete => "alert('hi')" ) %>
<span class="<%= if (task.done) then "editable_field_complete" else "editable_field" end %>" id="task_title_<%= task.id %>">
<%= best_in_place task, :title, :type => :input %>
</span>
</span>
<span class="taskdelete"><%= link_to "delete", task, :method => :delete, :class => "delete",
:confirm => "You sure?",
:title => task.title %></span>
<span class="taskcreated">
Created <%= time_ago_in_words(task.created_at) %> ago.
</span>
</td>
</tr>
非常感谢大家
最佳答案
问题似乎是remote_function使用POST协议(protocol)提交数据,但是当您在routes.rb中使用资源时,它在调用更新操作时只接受PUT协议(protocol)。
尝试将以下参数添加到您的remote_function中:
:method=>:put
所以最终结果将是:
<%= check_box_tag "id", "id", task.done,
:onclick => remote_function(
:update => "task",
:url => { :action => :update, :controller => :tasks, :id => task.id },
:method => :put,
:with => "task[done]=true",
:complete => "alert('hi')" ) %>
关于ruby-on-rails - ActionController::RoutingError: 使用复选框 + ajax 时没有路由匹配 ->,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5785249/