为什么一种方法起作用,另一种方法不起作用?

时间:2016-06-05 01:52:44

标签: ruby-on-rails ruby-on-rails-4

我是Rails的新手并创建了我自己的个人待办事项列表。在我看来,我有以下几点:

<% @todo_list.todo_items.each do |todo_item| %>
      <% if todo_item.completed? %>
      <tr id="<%= dom_id(todo_item) %>">
        <td><%= todo_item.completed_at.strftime("%a %-m/%-d/%y ") %><%= link_to "Mark Complete", uncomplete_todo_list_todo_item_path(todo_item), method: :patch %></td>
        <td><%= todo_item.weeklypriority %></td>

在我的控制器中我有:

  def complete
    @todo_item = @todo_list.todo_items.find(params[:id])
    @todo_item.update_attribute(:completed_at, Time.now)
    redirect_to todo_list_todo_items_path, notice: "Todo item marked as complete."
  end

  def uncomplete
    @todo_item = @todo_list.todo_items.find(params[:id])
    @todo_item.update_attribute(:completed_at, nil)
    redirect_to todo_list_todo_items_path, notice: "Todo item marked as uncomplete."
  end

当我使用complete_todo_list_todo_item_path(todo_item)时,事情就是花花公子。当我使用uncomplete_todo_list_todo_item_path(todo_item)时,我得到了

  

#&lt;#:0xb5509078&gt;

的未定义方法`uncomplete_todo_list_todo_item_path'

当我更改完整方法以执行与未完成方法相同的操作时,它运行良好。 complete不在任何帮助器中或设置为辅助方法,所以我很困惑为什么不完整不起作用。想法?

1 个答案:

答案 0 :(得分:0)

感谢pyfl88询问路线。我的路线看起来像这样:

Odot::Application.routes.draw do
  resources :todo_lists do
    resources :todo_items do
      member do
        patch :complete
      end
    end
  end
  root 'todo_lists#index'

所以在将 patch:uncomplete 添加到它工作的路由之后。