我正在构建一个简单的任务管理应用程序,当我试图处理复选框以指示任务完成时,会发生奇怪的问题。
下面是我想出来的代码:<%= 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并查看日志时,我看到以下条目:
开始POST "/tasks/20" for 127.0.0.1 at 2011-04-25 23:15:44 -0300ActionController::RoutingError (No route matches "/tasks/20"):
查看我的配置/路由。我有:
resources :tasks
你能帮我找出我做错了什么吗?为什么没有找到路线?
下面是_task.html的完整代码。erb 视图。
<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协议,但当你在路由中使用资源。那么在调用更新操作时,它只接受PUT协议。
尝试在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')" ) %>