【问题标题】:ActionController::RoutingError (No route matches [DELETE] "/tasks.1")ActionController::RoutingError (没有路由匹配 [DELETE] "/tasks.1")
【发布时间】:2015-06-25 01:42:52
【问题描述】:

我正在学习关于在本地环境中使用 Rails 4.2.1 的 TODO 应用程序的教程。

问题:/task.1,但应该是/task/1。由 Nithin 解决。 我还在 config/routes.rb 中将“resource :tasks”编辑为“:task”。

这些解决了“编辑”问题!!!

但在“删除”时仍然会出现某种路由错误。为什么?

Tasks_controller

class TasksController < ApplicationController
  respond_to :html, :js

  # GET /tasks
  # GET /tasks.json
  def new
    @task = Task.new    
    render :show_form
  end

  # POST /task
  # POST /task.json
  def create
    @task = Task.create(task_params)
    @tasks = Task.all
  end

  # GET /task
  # GET /task.json
  def edit
    @task = Task.find(params[:id])
    render :show_form
  end

  # DELETE /task
  # DELETE /task.json
  def destory
    @task = Task.find(params[:id])
    @task.destroy
    @tasks = Task.all
  end

  private

  def task_params
    params.require(:task).permit(:title, :note, :completed)
  end
end

_task_list.html.erb

<% if @tasks.empty? %>
  <span class="text-warning">There are no tasks!</span>
<% else %>
  <table class="table table-hover table-bordered">
    <thead>
        <tr>
            <th>Title</th>
            <th>Created at</th>
            <th>Completed</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
        <% @tasks.each do |task| %>
            <tr>
                <td><strong><%= task.title %></strong></td>
                <td class="text-info"><%= task.created_at %></td>
                <td class="text-success"><%= task.completed %></td>
                <td><%= link_to "Delete", task_path(id: task.id), method: :delete, remote: true, data: { confirm: "Are you sure you want to delete task #{task.title}?" }, class: "btn btn-primary" %>
                    <%= link_to "Edit", edit_task_path(id task.id), remote: true, class: "btn btn-primary" %>
                </td>
            </tr>
        <% end %>
    </tbody>
</table>

app/views/pages/home.html.erb

<div id="task-list" class="container">
  <%= render 'tasks/task_list', locals: {task: @task} %>
</div>
<div id="modal" class="modal fade"></div>

destroy.js.erb

$('#task-list').html('<%= j(render 'task_list', locals: {task: @task} )%>');

edit.js.erb

m = $('#modal');
m.html('<%= j(render 'task_form', locals: {task: @task}) %>');
m.modal('show');
$('#modal').integrateDatepicker();

config/routes.rb

Rails.application.routes.draw do
  # Tasks
  resource :tasks, only: [:new, :create, :edit, :destory]

  root to: 'pages#home'
end

日志:删除

Started DELETE "/task?id=1" for 127.0.0.1 at 2015-04-18 12:00:33 +0700

ActionController::RoutingError (没有路由匹配 [DELETE] "/task"): actionpack (4.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:21:in call' newrelic_rpm (3.11.2.286) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:incall' 网络控制台 (2.1.2) lib/web_console/middleware.rb:37:in call' newrelic_rpm (3.11.2.286) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:incall' actionpack (4.2.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'

...

耙路线

    Prefix    Verb URI Pattern           Controller#Action
    tasks     POST /tasks(.:format)      tasks#create
    new_task  GET  /tasks/new(.:format)  tasks#new
    edit_task GET  /tasks/edit(.:format) tasks#edit
    root      GET  /                     pages#home

【问题讨论】:

  • 发生在我们最好的人身上

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


【解决方案1】:

从这个线程的年龄来看,你要么已经想出如何解决这个问题,要么已经转向其他事情。

我注意到在这一行

resource :tasks, only: [:new, :create, :edit, :destory]

你拼错了destroy这个词。查看您的 rake 路由导出,我没有看到列出的 DELETE 操作。尝试将路由文件更新为此

resource :tasks, only: [:new, :create, :edit, :destroy]

【讨论】:

    【解决方案2】:

    链接应该是:

    <%= link_to "Delete", task_path(task), method: :delete, remote: true, data: { confirm: "Are you sure you want to delete task #{task.title}?" }, class: "btn btn-primary" %>
    <%= link_to "Edit", edit_task_path(task), remote: true, class: "btn btn-primary" %>
    

    【讨论】:

      【解决方案3】:

      这必须解决。

      在您的链接中尝试传递喜欢,

      编辑:

      edit_task_path(id: task.id)
      

      用于删除或显示页面

      task_path(id: task.id)
      

      【讨论】:

      • 谢谢,它解决了 id 问题。 TasksController#edit 作为 JS 参数处理:{"id"=>"1"} 任务负载 (0.2ms) SELECT "tasks".* FROM "tasks" WHERE "tasks"."id" = ?限制 1 [["id", 1]]
      • 但现在我得到了这个错误。 ActionView::Template::Error(#:0x007fc3a1402740> 的未定义方法 `task_path'):
      • 所以你原来的问题解决了吗?这是什么时候出现的?
      • 谢谢!我使用您的代码将其单数资源更改为复数控制器,“编辑”现在可以正常工作了!
      • 我是新用户,没有足够的声誉来投票支持您的答案。但我非常感谢你教 Nithin
      【解决方案4】:

      删除/编辑的链接错误。正确的是/tasks/1/tasks/edit/1

      在您的 erb 中,您提供了指向 link_to 的错误链接。

      <td><%= link_to "Delete", tasks_path(task), method: :delete, remote: true, data: { confirm: "Are you sure you want to delete task #{task.title}?" }, class: "btn btn-primary" %>
          <%= link_to "Edit", edit_tasks_path(task), remote: true, class: "btn btn-primary" %>
      </td>
      

      应该是task_path(task)edit_task_path(task),而不是task[s]_path

      【讨论】:

      • 然后我收到此错误:未定义方法 `task_path' for #:0x007ff5eac7f4d8>
      • 是的,所以我改成了单一资源。谢谢米峰!
      猜你喜欢
      • 2016-08-25
      • 2015-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多